数据库中有一个表 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 太多,我认为这不是一个好的解决方案。
答案 0 :(得分:2)
请注意,您想要的最大和最小纬度/经度方法选择当前位置的一个方框内的行,而不是如图所示,不在一个圆圈内。 挑战在于,对于给定距离,度数的纵向增量随纬度而变化。这段代码将地球简化为一个完美的领域。
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);