iPhone 开发 - CLLocationManager 与 MapKit

标签 iphone mapkit mkmapview cllocationmanager showuserlocation

如果我想在 map 上显示 userLocation,同时记录用户的位置,向 userLocation.location 添加观察者并记录位置是个好主意,还是应该使用 CLLocationManager 来记录用户location 并使用 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 entry :CLLocationManager 在其所有实例中使用相同的数据,因此不存在冲突。

关于iPhone 开发 - CLLocationManager 与 MapKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2590508/

相关文章:

iphone - 多次保存大型 nsstring 文件会使应用程序崩溃

ios - map 上 2 个给定点之间的 MKDirections 请求

ios - 位置标记不显示 Swift 4

ios - 我正在学习 iOS 开发并且有一个崩溃的 MapKit 应用程序。需要帮助理解这个崩溃日志

iphone - 倒计时 UILabel 不会递减

iphone - Xcode 调用应用 didFinishLaunchingWithOptions

Swift MapKit - 创建一个多边形

ios - 在 map 上隐藏一个特定的街道名称

ios - 路线未显示在 MKMapView 中?

iPhone/iPad : When to change background on rotate of device