经测试,按照该公式和腾讯地图经纬度距离计算,精度基本上是匹配的,返回的单位为:km。
地球半径采用的是6378.137千米。
//选取小余800km的经纬度; public function getNear() { global $db, $res; dbc(); $city_lat = "29.800712"; $city_lng = "121.556758"; $sql = "select toilet_id,toilet_name,toilet_address,toilet_url,toilet_lon,toilet_lat,ROUND((6378.137 * acos (cos (radians($city_lat) )* cos(radians( toilet_lat ) )* cos(radians( toilet_lon ) - radians($city_lng) )+ sin (radians($city_lat) )* sin(radians( toilet_lat ))))) AS distance from " . $db->table('toilet') . " HAVING distance < 800 ORDER BY distance ASC"; $row = $db->queryall($sql); $res["data"] = $row; die(json_encode_lockdata($res)); }
{ "err": "", "res": "", "data": [ { "toilet_id": "5", "toilet_name": "单县东大医院", "toilet_address": "山东省菏泽市单县舜师东路1号", "toilet_url": "upload/preview/2020-11/3e29ff1cb9f04edc889f19962e05cf57.jpg", "toilet_lon": "116.109825", "toilet_lat": "34.775480", "distance": "754" }, { "toilet_id": "1", "toilet_name": "舜师路中段公厕", "toilet_address": "山东省菏泽市单县舜师路", "toilet_url": "upload/nopic.jpg", "toilet_lon": "116.090331", "toilet_lat": "34.778643", "distance": "756" }, { "toilet_id": "4", "toilet_name": "文化路公厕", "toilet_address": "单县舜师路与文化路交叉口西南方向20米", "toilet_url": "upload/preview/2020-11/627365751e1c62eb98d8f4ae472efc81.jpg", "toilet_lon": "116.087551", "toilet_lat": "34.780302", "distance": "756" } ] }
Done !