使用MKMapView时如何设置精度和距离过滤器 [英] How to set accuracy and distance filter when using MKMapView
问题描述
当我使用 setShowsUserLocation
和 MKMapView
跟踪用户位置时,如何设置精度和距离过滤器?我不是在谈论 CLLocationManager
.
When i use setShowsUserLocation
with MKMapView
to track user location, how do I set the accuracy and distance filter? I am not talking about CLLocationManager
.
谢谢,
推荐答案
您无法控制内部 MKMapView
位置管理器(用于使用蓝点跟踪用户的位置管理器)的准确性,但您可以创建自己的地图并使用它来重新定位地图.这是一个食谱...
You can't control the accuracy of the internal MKMapView
location manager (the one used to track the user with the blue dot), but you can create your own and use it to recenter the map. Here is a recipe...
在核心位置委托中:
- (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);
}
}
就在设置位置管理器之前:
Right before setting up the location manager:
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 没有距离或精度过滤器,只有 CLLocationManager 有.MKMapView 具有的是围绕一个点的区域跨度,在上面的示例中为 0.15 度(0.15*111 公里).
Put that on the delegate. MKMapView doesn't have a distance or accuracy filter, only the CLLocationManager does. What MKMapView has is a region span around a point, in the example above 0.15 degrees (0.15*111 Km).
文档没有说明 MKMapView
从哪里获取更新.我试过了
The documentation doesn't tell where MKMapView
is getting its updates from. I tried
- (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
,这意味着您无法设置其准确性.你也不能为 MKMapView
的 CLLocationManager
添加你的委托.
and I'm getting different values on each. It looks as if MKMapView
uses its own CLLocationManager
, which means you can't set its accuracy. You can't add your delegate for the CLLocationManager
of the MKMapView
either.
我的印象是设置精度的唯一方法是将显示用户位置设置为 NO 并创建带有蓝点的自定义注释,这意味着在发布时手动重新定位地图.您可以使用github项目artwork-extractor从SDK中获取蓝点图形.
My impression is that the only way to set the accuracy is setting show user position to NO and create a custom annotation with a blue dot, which means recentering the map manually as posted. You can get the blue dot graphic from the SDK with the github project artwork-extractor.
我不知道是我遗漏了什么,还是 MKMapView
的这部分很烂.
I don't know if I'm missing something or this part of MKMapView
just sucks.
这篇关于使用MKMapView时如何设置精度和距离过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!