gpt4 book ai didi

iphone - 在纬度/经度列表中查找纬度和经度的最接近匹配项

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:16:13 25 4
gpt4 key购买 nike

我正在创建一个具有特定位置天气查询功能的 iPhone 应用程序,但我遇到以下问题,我不确定解决的最佳方法。

我有一个位置的纬度和经度,想从 5000 多个位置的列表中找到最接近的纬度/经度匹配项

5000 多个位置来自 Met Office Datapoint API 的 JSON 提要,采用 NSDictionaries 的 NSArray 形式,NSDictionary 包括 id、lat、long 和名称。

我想将我的位置与英国气象局列表中最近的位置相匹配,并获取 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com