iphone - 使用MKMapView时如何设置精度和距离过滤器

标签 iphone ios ipad mkmapview cllocationmanager

当我使用 setShowsUserLocationMKMapView 来跟踪用户位置时,如何设置精度和距离过滤器?我不是在谈论 CLLocationManager

谢谢,

最佳答案

您无法控制内部 MKMapView 位置管理器(用于用蓝点跟踪用户的那个)的准确性,但您可以创建自己的并使用它来重新定位 map .这是一个食谱...

处理核心位置权限

在核心位置委托(delegate)中:

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied){
        NSLog(@"User has denied location services");
    } else {
        NSLog(@"Location manager did fail with error: %@", error.localizedFailureReason);
    }
}

在设置位置管理器之前:

if (![CLLocationManager locationServicesEnabled]){
    NSLog(@"location services are disabled"];
    return;
}
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied){
    NSLog(@"location services are blocked by the user");
    return;
}
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized){
    NSLog(@"location services are enabled");
}  
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined){
    NSLog(@"about to show a dialog requesting permission");
}

设置核心位置

self.locationManager = [CLLocationManager new];
self.locationManager.purpose = @"Tracking your movements on the map.";
self.locationManager.delegate = self;

/* Pinpoint our location with the following accuracy:
 *
 *     kCLLocationAccuracyBestForNavigation  highest + sensor data
 *     kCLLocationAccuracyBest               highest     
 *     kCLLocationAccuracyNearestTenMeters   10 meters   
 *     kCLLocationAccuracyHundredMeters      100 meters
 *     kCLLocationAccuracyKilometer          1000 meters 
 *     kCLLocationAccuracyThreeKilometers    3000 meters
 */
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;

/* Notify changes when device has moved x meters.
 * Default value is kCLDistanceFilterNone: all movements are reported.
 */
self.locationManager.distanceFilter = 10.0f;

/* Notify heading changes when heading is > 5.
 * Default value is kCLHeadingFilterNone: all movements are reported.
 */
self.locationManager.headingFilter = 5;

// update location
if ([CLLocationManager locationServicesEnabled]){
    [self.locationManager startUpdatingLocation];
}

使用我们的位置管理器使 map 重新居中

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
           fromLocation:(CLLocation *)oldLocation 
{
    MKCoordinateRegion region = { { 0.0f, 0.0f }, { 0.0f, 0.0f } };
    region.center = newLocation.coordinate;
    region.span.longitudeDelta = 0.15f; 
    region.span.latitudeDelta = 0.15f;
    [self.mapView setRegion:region animated:YES];
}

把它放在委托(delegate)上。 MKMapView 没有距离或精度过滤器,只有 CLLocationManager 有。 MKMapView 拥有的是围绕一个点的区域跨度,在上面的示例中为 0.15 度(0.15*111 公里)。

我尝试过但没有奏效的事情

文档没有说明 MKMapView 从哪里获得更新。我试过了

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
           fromLocation:(CLLocation *)oldLocation {
    NSLog(@"newLocation %@", newLocation.timestamp);
    NSLog(@"last map location %@", [NSString stringWithFormat:@"%@",[[[self.mapView userLocation] location] timestamp]]);
}

我对每个值都有不同的值。看起来 MKMapView 使用它自己的 CLLocationManager,这意味着您无法设置它的准确性。您也不能为 MKMapViewCLLocationManager 添加委托(delegate)。

我的印象是,设置精度的唯一方法是将“显示用户位置”设置为“否”并创建带有蓝点的自定义注释,这意味着手动将 map 重新​​居中。您可以使用 github 项目 artwork-extractor 从 SDK 中获取蓝点图形。

我不知道是我遗漏了什么,还是 MKMapView 的这一部分很糟糕。

关于iphone - 使用MKMapView时如何设置精度和距离过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5930612/

相关文章:

ios - 使用带有对象的协议(protocol)来获取对象的实例

ios - 使用 swiftvalidator 的 swift 正则表达式出错?

jquery - 什么是用于关闭屏幕键盘的 Mobile Safari 事件?

iphone - UIScrollView 中的动画

iphone - 使用自定义转场转换时 TabBar 会丢失。如何取回?

ios - 修复覆盖函数 viewDidAppear

iphone - 在 iOS 音频播放器中改变音高——比如 Alvin and the Chipmunks

ios - 如何获取iPhone中图库中选择的图像的路径?

iphone - 带有滑动问题的旋转圆圈

iphone - 提供潜在的内存泄漏警告和应用程序崩溃