【问题标题】:Finding the Closest CLLocation寻找最近的CLLocation
【发布时间】:2013-07-27 10:15:31
【问题描述】:

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

【问题讨论】:

    标签: iphone ios cllocationmanager cllocation currentlocation


    【解决方案1】:
    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);
    

    【讨论】:

    • 更新closestDistance时需要更新closestLocation
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 2011-05-09
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    相关资源
    最近更新 更多