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

当我用MKMapView setShowsUserLocation跟踪用户的位置,我该如何设置精度和距离filter? 我不是在谈论CLLocationManager

谢谢,

您无法控制内部MKMapView位置pipe理器(用于使用蓝点跟踪用户的位置pipe理器)的准确性,但您可以创build自己的地图并使用它重新映射地图。 这是一个配方…

处理核心位置权限

在核心位置代表:

 - (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); } } 

在设置位置pipe理员之前:

 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]; } 

与我们的位置经理重新接收地图

 - (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]; } 

把这个放在委托上 MKMapView没有距离或精度filter,只有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

我的印象是设置精确度的唯一方法是将显示用户位置设置为NO,并用蓝点创build自定义注释,这意味着以张贴的方式手动重新映射地图。 您可以使用github项目的图稿提取器从SDK中获取蓝点graphics。

我不知道我是否遗漏了一些东西,或者MKMapView这个部分太糟糕了。

您无法设置准确性,但是,您可以通过MKMapView委托方法中的userLocation获取准确性。

 - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { NSLog(@"%f", userLocation.location.horizontalAccuracy); } 

这里显示地图是一个示例代码

首先在你的.h文件中导入MKMapKit和CoreLocation框架。

 #import <MapKit/MapKit.h> #import <CoreLocation/CoreLocation.h> 

在.h文件中添加MKMapKit和CoreLocation委托

 @interface MapViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate> CGPoint gameMapCenter = CGPointMake([[UIScreen mainScreen] bounds].size.width / 2, [[UIScreen mainScreen] bounds].size.height / 2); gameMapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 640, 620)]; [gameMapView setCenter:gameMapCenter]; [gameMapView setMapType:MKMapTypeStandard]; [gameMapView setDelegate:self]; [self.view addSubview:gameMapView]; [gameMapView setShowsUserLocation:YES]; 

使用CLLocationManager获取用户位置。

声明CLLocationManager的一个实例

 CLLocationManager *locationManager; 

ViewDidLoad

 locationManager = [[CLLocationManager alloc] init]; [locationManager setDelegate:self]; [locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; [locationManager setDistanceFilter:kCLDistanceFilterNone]; [locationManger startUpdatingLocation]; 

startUpdatingLocation方法实现:

 (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { //Your Stuff }