[英]How to sort locations based on distance using google maps
I have a database in following schema: 我有一个以下架构的数据库:
- id INT (PRIMARY)
- address (TEXT)
- zipcode (VARCHAR)
- latitude
- longitude
What I want: is to have a function in PHP which, on passing a zipcode as a parameter, can sort out all the entries in above table based on nearest distance. 我想要的是:在PHP中有一个函数,在传递一个zipcode作为参数时,可以根据最近距离整理上表中的所有条目。
What I've tried: I know about Haversine formula which can be used in following way to fetch from SQL (as also described in this google documentation ): 我尝试了什么:我知道可以通过以下方式从SQL获取的Haversine公式 (如本google文档中所述 ):
SELECT *,
( 3959 * acos( cos( radians('$lat') ) *
cos( radians( latitude ) ) *
cos( radians( longitude ) -
radians('$lon') ) +
sin( radians('$lat') ) *
sin( radians( latitude ) ) ) )
AS distance FROM my_table HAVING distance < '$miles' ORDER BY distance ASC
$lat
, $lon
, $miles
can be replaced with actual latitude, longitude and miles to search within, respectively $lat
, $lon
, $miles
可以分别用实际的纬度,经度和里程来代替
Results: Using above formula output me result which seemed bit off from what I got from google maps. 结果:使用上面的公式输出我的结果似乎与我从谷歌地图得到的一点点。 For ex- for zipcode 00601 and 00631 , The distance this query provided was 5.432822479090713 miles , while using google map api, the distance for same was 10.00408 miles .
对于例如邮政编码00601和00631 ,此查询提供的距离为5.432822479090713英里 ,而使用谷歌地图api时,距离为10.00408英里 。
http://maps.googleapis.com/maps/api/distancematrix/json?origins=00601&destinations=00631&mode=DRIVING&sensor=false http://maps.googleapis.com/maps/api/distancematrix/json?origins=00601&destinations=00631&mode=DRIVING&sensor=false
While I can understand that earth being not a perfect sphere as well as the distance I am getting is only true in geometry sense, while google accounts for terrains, routes etc., this will not fit my use-case where many locations are nearby (within 2 mile radius). 虽然我可以理解地球不是一个完美的球体以及我得到的距离只是几何意义上的真实,而谷歌占地形,路线等,这不适合我的用例,附近有很多地方(在2英里范围内)。 I know a similar site which implements this is better way, I just wanted to know what possible solution they might've used?
我知道一个类似的网站实现这个更好的方式,我只是想知道他们可能使用的解决方案是什么?
EDIT - The reason I have opted for google maps to sort the location is basically due to its precise distance calculations between location for small distances 编辑 - 我选择谷歌地图对位置进行排序的原因主要是由于它在小距离位置之间的精确距离计算
I have a solution calculating distance in KM using the Haversine Formula , which I convert to miles by multiplying 0.621. 我有一个使用Haversine公式计算距离的解决方案,我通过乘以0.621转换为英里。
SELECT *, ( 6371 * 2 * atan2(SQRT(POW(sin((radians(geo_lat1)-radians(geo_lat2))/2), 2) + cos(radians(geo_lat2)) * cos(radians(geo_lat1)) * POW(sin((radians(geo_lng1)-radians(geo_lng2))/2),2))
,SQRT(1 - POW(sin((radians(geo_lat1)-radians(geo_lat2))/2), 2) + cos(radians(geo_lat2)) * cos(radians(geo_lat1)) * POW(sin((radians(geo_lng1)-radians(geo_lng1))/2),2)))*0.621)
as distance FROM my_table HAVING distance < '$miles' ORDER BY distance ASC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.