使用MKMapView时如何设置精度和距离滤镜

Van*_*ran 29 iphone mkmapview cllocationmanager ipad ios

当我使用setShowsUserLocationMKMapView跟踪用户的位置,我该如何设置的准确性和距离过滤器?我不是在谈论CLLocationManager.

谢谢,

Jan*_*ano 81

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

处理核心位置权限

在核心位置代表:

- (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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在设置位置管理器之前:

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");
}
Run Code Online (Sandbox Code Playgroud)

设置核心位置

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];
}
Run Code Online (Sandbox Code Playgroud)

使用我们的位置管理器重新定位地图

- (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];
}
Run Code Online (Sandbox Code Playgroud)

把它放在代表身上.MKMapView没有距离或精度过滤器,只有CLLocationManager.MKMapView具有的是一个点周围的区域,在0.15度(0.15*111 Km)以上的示例中.

我尝试过但没有用的东西

该文档没有说明从何处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]]);
}
Run Code Online (Sandbox Code Playgroud)

而且每个人都有不同的价值观.它看起来好像MKMapView使用了自己的CLLocationManager,这意味着你无法设置其准确性.您无法CLLocationManager为其中MKMapView任何一个添加委托.

我的印象是,设置精确度的唯一方法是将显示用户位置设置为NO并使用蓝点创建自定义注释,这意味着在发布时手动重新定位地图.您可以使用github项目的图形提取器从SDK获取蓝点图形.

我不知道我是否遗漏了某些东西或者这部分内容MKMapView很糟糕.