: This article mainly introduces PHP, Mysql-query nearby locations based on a given latitude and longitude point. if you are interested in PHP tutorials, refer to it.
Define (EARTH_RADIUS, 6371); // Earth radius, the average radius is 6371 km/***, and the four points of the square with a certain distance around a certain latitude and longitude are calculated ** @ param lng float longitude * @ param lat float latitude * @ param distance float radius of the circle where the point is located, this circle is tangent to this square. the default value is 0.5 km * @ return array coordinate of the longitude and latitude of the four points of the square */function returnSquarePoint ($ lng, $ lat, $ distance = 0.5) {$ dlng = 2 * asin (sin ($ distance/(2 * EARTH_RADIUS)/cos (deg 2rad ($ lat); $ dlng = rad2deg ($ dlng ); $ dlat = $ distance/EARTH_RADIUS; $ dlat = rad 2deg ($ dlat); return array ('left-top' => array ('lat' => $ lat + $ dlat, 'lng '=> $ lng-$ dlng), 'right-top' => array ('lat' => $ lat + $ dlat, 'lng '=> $ lng + $ dlng), 'left-bottom' => array ('lat' => $ lat-$ dlat, 'lng '=> $ lng-$ dlng), 'right-bottom' => array ('lat' => $ lat-$ dlat, 'lng '=> $ lng + $ dlng);} // use this function to calculate the result and bring it to the SQL query. $ Squares = returnSquarePoint ($ lng, $ lat); $ info_ SQL = "select id, locateinfo, lat, lng from 'lbs _ info' where lat <> 0 and lat >{$ squares ['right-bottom '] ['lat']} and lat <{$ squares ['left -top '] ['lat']} and lng> {$ squares ['left-top'] ['lng']} and lng <{$ squares ['right-bottom '] ['lng']} ";