pra*_*phy 0 iphone objective-c geolocation latitude-longitude ios
我有一个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;
}
}
Run Code Online (Sandbox Code Playgroud)
需要注意的一点是,这种计算距离的方法使用A点和B点之间的直线.没有考虑道路或其他地理对象.
归档时间: |
|
查看次数: |
2910 次 |
最近记录: |