简体   繁体   English

PHP / MySQL - 查找距实际gps坐标500米半径范围内的所有项目

[英]PHP / MySQL - Find all items in 500 meters radius from actual gps coordinates

I'm doing some compare of prices in same location based on GPS coordinates. 我正在根据GPS坐标比较相同位置的价格。 So eg I've got an item with coordinates: 所以例如我有一个带坐标的项目:

lat: 45.815005 
lng: 15.978501

I want to search in my MySQL database for each items which are eg 500 meters around that place. 我想在我的MySQL数据库中搜索每个项目,例如那个地方500米。 (it doesn't need to be circle, just 500 meters on X and 500 meters on Y both ways). (它不需要是圆圈,在X上只有500米,在Y上只有500米)。 Lat and lng are stored as a separate columns type float(10,6) in my DB Latlng在我的数据库中存储为单独的列类型float(10,6)

I am aware of the fact it's not easy to calculate exact lng and lat but I'm fine if I miss few meters each site. 我知道计算精确的lng和lat并不容易,但如果我错过每个站点几米,我就没事了。

This is pretty complex question but I would be thankful for any advise which will kickoff my start. 这是一个非常复杂的问题,但我会感谢任何建议,这将启动我的开始。

Calculating the distance between two coordinates isn't actually that difficult given the haversine formula. 考虑到半正式公式,计算两个坐标之间的距离实际上并不困难。

SELECT 
  -- stuff here
  , ( 6371000 * acos( cos( radians(45.815005) ) * cos( radians( stuff.lat ) ) * cos( radians( stuff.lng ) - radians(15.978501) ) + sin( radians(45.815005) ) * sin(radians(stuff.lat)) ) ) AS distance 
FROM 
  stuff
HAVING 
  distance < 500

Referenced Answer 参考答案

Necessary changes from the original answer: 原始答案需要进行必要的修改:

  1. The constant offered in the original answer supplied the values for miles or kilometers. 原始答案中提供的常数提供了英里或公里的值。 I've changed the constant here to work with meters. 我已经改变了常量来处理米。

  2. The constants have changed to use your coordinates. 常量已更改为使用您的坐标。 You might want to adapt the query a little further to make those parameters instead of constants. 您可能希望进一步调整查询以使这些参数而不是常量。

  3. The having expression changed a little to reflect your desire for 500 meters. having表达变化不大,以反映您的约500米的欲望。 Again, this might be something you want to parameterize. 同样,这可能是您想要参数化的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM