iPhone开发 - CLLocationManager与MapKit

时间:2010-04-07 06:49:08

标签: iphone mapkit mkmapview cllocationmanager showuserlocation

如果我想在地图上显示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都会尝试使用相同的位置服务吗?会不会有任何冲突,缺乏准确/必需或最新的数据?

1 个答案:

答案 0 :(得分:1)

请参阅此SO entry:CLLocationManager在其所有实例中使用相同的数据,因此不存在冲突。