我正在创建一个 iPhone 应用程序,用于查找特定位置的天气,但我遇到了以下问题,我不确定解决的最佳方法。
我有某个位置的纬度和经度,并希望从 5000 多个位置的列表中找到最接近的纬度/经度匹配
5000 多个位置来自 Met Office Datapoint API 的 JSON 提要,并采用 NSDictionaries 的 NSArray 形式,NSDictionary 包括 id、lat、long 和 name。
我想将我的位置与英国气象局列表中最近的位置进行匹配,并获取 id 键值。
提前非常感谢
最佳答案
我假设您在此使用 CLLocation 对象...
- (CLLocation*)closestLocationToLocation:(CLLocation*)currLocation
{
CLLocationDistance minDistance;
CLLocation *closestLocation = nil;
for (CLLocation *location in arrayOfLocations) {
CLLocationDistance distance = [location distanceFromLocation:currLocation];
if (distance <= minDistance
|| closestLocation == nil) {
minDistance = distance;
closestLocation = location;
}
}
//closestLocation is now the location from your array which is closest to the current location or nil if there are no locations in your array.
return closestLocation;
}
可能有一种更快的方法来做到这一点,但这将完成它。
编辑使用 CLLocation 函数
关于iphone - 在纬度/经度列表中查找最接近的纬度和经度匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14727720/