gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 07:49:30 24 4
gpt4 key购买 nike

当我使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com