gpt4 book ai didi

iphone - 如何从位置列表中获取最近的区域?

转载 作者:行者123 更新时间:2023-11-28 18:24:59 26 4
gpt4 key购买 nike

我有一个 API,它返回城市内不同区域的列表以及该区域的天气。我想根据我当前的位置获取最近的区域。

API 返回

  • 地区
  • 纬度
  • 经度
  • 天气

如何根据这些数据找到最近的区域?

最佳答案

您必须为所有区域创建 CLLocation 对象,再加上一个用于用户当前位置的对象。然后使用类似于下面的循环来获取最近的位置:

NSArray *allLocations; // this array contains all CLLocation objects for the locations from the API you use

CLLocation *currentUserLocation;

CLLocation *closestLocation;
CLLocationDistance closestLocationDistance = -1;

for (CLLocation *location in allLocations) {

if (!closestLocation) {
closestLocation = location;
closestLocationDistance = [currentUserLocation distanceFromLocation:location];
continue;
}

CLLocationDistance currentDistance = [currentUserLocation distanceFromLocation:location];

if (currentDistance < closestLocationDistance) {
closestLocation = location;
closestLocationDistance = currentDistance;
}
}

需要注意的是,这种计算距离的方法使用 A 点和 B 点之间的直线。没有考虑道路或其他地理对象。

关于iphone - 如何从位置列表中获取最近的区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12027558/

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