使用MySQL空间数据在Google地图上获取最近的地点 [英] Get nearest places on Google Maps, using MySQL spatial data

查看:134
本文介绍了使用MySQL空间数据在Google地图上获取最近的地点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,每个数据库都有一个纬度和经度的商店列表。因此,根据我输入的当前(lat,lng)位置,我想获得一些半径范围内的物品列表,例如1公里,5公里等等。

算法应该是什么?我需要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。



您也可以这样做:


  1. 选择所有经度和纬度然后计算每条记录的距离。

  2. 上面的过程可以用多个重定向。

为了优化查询,您可以使用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:

  1. Select all Latitude and longitude

  2. Then calculate the distance for each record.

  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆