【问题标题】:How to get locations in the range of 1KM nearby current_location?如何获取current_location附近1KM范围内的位置?
【发布时间】:2014-02-19 05:53:47
【问题描述】:

数据库中有一个表location

id | latitude | longitude
---| -------- | --------
1  | 45.0000  | 90.0000
...

所以我可以使用 SQL 来查询我想要的位置:

SELECT id FROM location 
WHERE latitude BETWEEN min_latitude AND max_latitude
AND longitude BETWEEN min_longitude AND max_longitude;

其中min_latitude,max_latitude,min_longitude,max_longitude表示current_location附近1KM的4个边界。

现在我想知道,如何使用 current_location 获取 min_latitude、max_latitude、min_longitude、max_longitude?​​p>

我看过这个问题的答案:How to get the nearest area from a list of locations?,如果allLocations太多,我觉得不是很好的解决方案。

【问题讨论】:

标签: ios sqlite location


【解决方案1】:

请注意,您希望使用最大和最小纬度/经度的方法选择从当前位置开始的框内的行,而不是如图所示的圆圈内的行。
挑战在于,对于给定距离,以度数为单位的纵向三角洲随纬度而变化。这段代码将地球简化为一个完美的球体。

    double centerLat = 45.0;
    double centerLon = 90.0;

    double d = 1000; // desired distance in meters

    double angle = 45 * M_PI / 180; // 45 degrees in radians
    double oneDegree = 111319.9; // distance in meters from degree to degree at the equator

    double maxLat = centerLat + (d / oneDegree) * sin(angle);
    double maxLon = centerLon + (d / (oneDegree*(cos(centerLat * M_PI / 180)))) *  cos(angle);
    double minLat = centerLat - (d / oneDegree)* sin(angle);
    double minLon = centerLon - (d / (oneDegree*(cos(centerLat * M_PI / 180)))) *  cos(angle);

【讨论】:

  • 你好,你如何得到常量double oneDegree = 111319.9
  • 我在 wikipedia 中查到了 :-)。如果将地球的周长除以 360,这就是你得到的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
相关资源
最近更新 更多