使用MySQL空间数据在Google地图上获取最近的地点 [英] Get nearest places on Google Maps, using MySQL spatial data
问题描述
算法应该是什么?我需要PHP代码来实现算法本身。
您只需使用以下查询。
例如,您输入经度和纬度37和-122度。您希望在距离当前经纬度25英里的范围内搜索用户。
SELECT item1,item2,
(3959 * acos(cos(弧度(37))
* cos(弧度(lat))
* cos(弧度(lng)
- 弧度(-122))
+ sin(弧度(37))
* sin(弧度(lat))
)
)AS距离
FROM地理编码表
HAVING距离< 25
ORDER BY距离限制0,20;
如果您想以kms为单位搜索距离,则在上面的查询中用6371替换3959。
您也可以这样做:
-
选择所有经度和纬度然后计算每条记录的距离。
-
上面的过程可以用多个重定向。
为了优化查询,您可以使用Stored Procedure。
这也可以帮助你。
I have a database with a list of stores with latitudes and longitudes of each. So based on the current (lat, lng) location that I input, I would like to get a list of items from those within some radius like 1 km, 5km etc?
What should be the algorithm? I need the PHP code for algorithm itself.
You just need use following query.
For example, you have input latitude and longitude 37 and -122 in degrees. And you want to search for users within 25 miles from current given latitude and longitude.
SELECT item1, item2,
( 3959 * acos( cos( radians(37) )
* cos( radians( lat ) )
* cos( radians( lng )
- radians(-122) )
+ sin( radians(37) )
* sin( radians( lat ) )
)
) AS distance
FROM geocodeTable
HAVING distance < 25
ORDER BY distance LIMIT 0 , 20;
If you want search distance in kms, then replace 3959 with 6371 in above query.
You can also do this like:
Select all Latitude and longitude
Then calculate the distance for each record.
The above process can be done with multiple redirection.
For optimizing query you can use Stored Procedure.
And this can also help you.
这篇关于使用MySQL空间数据在Google地图上获取最近的地点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!