如果我想在地图上显示 userLocation 并同时记录用户的位置,将观察者添加到 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 都会尝试使用相同的位置服务?是否会有任何冲突和缺乏准确/必需或当前的数据?