Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 查找最近的位置_Iphone_Ios_Cllocationmanager_Cllocation_Currentlocation - Fatal编程技术网

Iphone 查找最近的位置

Iphone 查找最近的位置,iphone,ios,cllocationmanager,cllocation,currentlocation,Iphone,Ios,Cllocationmanager,Cllocation,Currentlocation,如何找到离用户最近的位置?基本上,我有一堆CLLocation,我想找到最接近用户的一个。我已检索到用户的当前位置,但我想找到最近的位置。我该怎么做呢?您可以只记录最近的距离。更新ClosesLocation时需要更新ClosesDistance更新ClosesLocation时需要更新ClosesDistance CLLocation *currentLocation; NSArray *otherLocations; CLLocation *closestLocation; CLLocat

如何找到离用户最近的位置?基本上,我有一堆CLLocation,我想找到最接近用户的一个。我已检索到用户的当前位置,但我想找到最近的位置。我该怎么做呢?您可以只记录最近的距离。

更新ClosesLocation时需要更新ClosesDistance更新ClosesLocation时需要更新ClosesDistance
CLLocation *currentLocation;
NSArray *otherLocations;

CLLocation *closestLocation;
CLLocationDistance closestDistance = DBL_MAX;

for (CLLocation* location in otherLocations) {
    CLLocationDistance distance = [currentLocation distanceFromLocation:location];
    if (distance < closestDistance) {
        closestLocation = location;
        closestDistance = distance;
    }
}
NSLog(@"the closest location is %@", closestLocation);