获取用户最终位置 IOS



我正在下面做一个查询:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    if (!gotLocation){
        gotLocation = true;
        [locationManager stopUpdatingLocation];
        // DO QUERY
    }
}

但我想知道何时确定最终位置,或者何时制作更准确的位置。我只尝试查询一次,需要查询的精确位置。我知道我可以使用[locations lastObject],但是我如何确定何时设置最终或更精确的位置。

您可以查看该位置的horizontalAccuracy。值越小越准确。您应该保留对上次收到的位置的引用,然后当出现更准确的新位置时,horizontalAccuracy更小,更新您的引用。

然后,一旦您收到一个位置,其horizontalAccuracy距离您的用例足够近,您就可以致电stopUpdatingLocation,因为您有自己的位置,然后进行查询。

例如:

CLLocation *location = [locations lastObject];
if (location.horizontalAccuracy < self.lastLocation.horizontalAccuracy) {
    self.lastLocation = location;
    if (self.lastLocation.horizontalAccuracy <= kDesiredLocationAccuracy) {
        // execute query or additional logic
        [locationManager stopUpdatingLocation];
    }
}

希望对您有所帮助!

最新更新