简体   繁体   English

如何使用谷歌地图基于距离对位置进行排序

[英]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 . 对于例如邮政编码0060100631 ,此查询提供的距离为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.

相关问题 使用Google Maps Api(JSON)获取两个位置之间的距离 - Getting the distance between two locations using Google Maps Api (JSON) 使用Google Maps API查找两个位置之间的路线和距离,并将其显示在表格中 - Using Google Maps API to find routes & distance between two locations and present it in a table 如何使用Google Maps API根据距离进行搜索? - How can I search based on distance with the Google Maps API? 使用PHP的Google Maps API查找两个位置之间的距离 - Google Maps API with PHP to find distance between two locations 如何使用Google地图获取距离? - How can I get the distance using google maps? 确定和排序几个位置的距离 - determine and sort distance for several locations 使用SQL查询根据地理编码之间的距离选择位置 - Select locations based on distance between geocodes using SQL query 如何使用Google Maps API定义多个位置,从而为循环生成的帖子列表中的每个帖子添加图钉 - How to define multiple locations Using Google Maps API to drop pin for every post in a list of posts generated by a loop 如何从数据库mysql获取位置并使用Google Maps上的标记来精确定位位置 - how to fetch a location from database mysql and pinpoint the locations using markers on google maps 如何使用PHP和数据库中的位置在Google地图上设置点? - How can I set points on google maps, using php and locations from database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM