开发者

iPhone Development - CLLocationManager vs. MapKit

开发者 https://www.devze.com 2022-12-26 13:23 出处:网络
If i want to show userLocation on the map, and at the same time record the user\'s location, is it a good idea to add an observer to userLocation.location and record the locations, OR should i still u

If i want to show userLocation on the map, and at the same time record the user's location, is it a good idea to add an observer to userLocation.location and record the locations, OR should i still use CLLocationManager for recording user location and use mapView.showUserLocation to show the user's current location (blue indicator)? I want to show the default blue indicator supported by the MapKit API.

Also, here's a rough sample code:

- (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 l开发者_如何学JAVAocation information
    // ...
}

Under the hood, i think MKMapView also uses CLLocationManager to get user's current location? So, will this create any problems because i believe both CLLocationManager and MapView will try to use same location services? Will there be any conflicts and lack of accurate/required or current data?


See this SO entry: CLLocationManager uses the same data across all of its instances, so there is no conflict.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号