如果我想在地图上显示用户位置,并在同一时间记录用户的位置,它是一个好主意,添加一个观察者userLocation.location并记录位置,或者我应该仍然使用CLLocationManager用于记录用户位置和使用mapView.showUserLocation显示用户的当前位置(蓝色指示灯)?我想显示由MapKit API支持的默认蓝指示剂。

此外,这里有一个粗糙的示例代码:

- (void)viewDidLoad {
    ...

    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = DISTANCE_FILTER_VALUE;
    locationManager.delegate = self; 
    [locationManager startUpdatingLocation];

    myMapView.showUserLocation = YES;
    [myMapView addObserver:self forKeyPath:@"userLocation.location" options:0 context:nil];

    ...
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    // Record the location information
    // ...
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    NSLog(@"%s begins.", __FUNCTION__);

    // Make sure that the location returned has the desired accuracy
    if (newLocation.horizontalAccuracy <= manager.desiredAccuracy)
        return;

    // Record the location information
    // ...
}

引擎盖下,我想的MKMapView还使用CLLocationManager获得用户的当前位置?因此,这将产生任何问题,因为我相信无论CLLocationManager和MapView的将尝试使用同一位置的服务?会有任何冲突和缺乏准确/或所需的当前数据的?

有帮助吗?

解决方案

请参阅此 SO条目:CLLocationManager在其所有实例的使用相同的数据,所以有没有冲突。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top