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

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

问题描述

我有一个数据库,里面有一个商店列表,每个商店的纬度和经度.因此,根据我输入的当前(纬度、经度)位置,我想从某个半径(例如 1 公里、5 公里等)内的项目中获取项目列表?

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?

算法应该是什么?我需要算法本身的 PHP 代码.

What should be the algorithm? I need the PHP code for algorithm itself.

推荐答案

您只需要使用以下查询.

You just need use following query.

例如,您输入了纬度和经度 37 以及 -122 的度数.并且您想搜索距当前给定的经纬度 25 英里范围内的用户.

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;

如果你想要以公里为单位的搜索距离,那么在上面的查询中将 3959 替换为 6371.

If you want search distance in kms, then replace 3959 with 6371 in above query.

你也可以这样做:

  1. 选择所有纬度和经度

  1. 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.

而且这个也可以帮助您.

这篇关于使用 MySQL 空间数据获取 Google 地图上最近的地点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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