ios 使用MKMapView时如何设置精度和距离过滤器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5930612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set accuracy and distance filter when using MKMapView
提问by Van Du Tran
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
.
当我使用setShowsUserLocation
withMKMapView
跟踪用户位置时,如何设置精度和距离过滤器?我不是在谈论CLLocationManager
。
Thanks,
谢谢,
回答by Jano
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...
您无法控制内部MKMapView
位置管理器(用于使用蓝点跟踪用户的位置管理器)的准确性,但您可以创建自己的位置管理器并使用它来重新定位地图。这是一个食谱...
To handle core location permissions
处理核心位置权限
In the Core Location 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);
}
}
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");
}
To setup core location
设置核心位置
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];
}
To recenter the map with our location manager
使用我们的位置管理器重新调整地图
- (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];
}
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 没有距离或精度过滤器,只有 CLLocationManager 有。MKMapView 具有的是围绕一个点的区域跨度,在上面的示例中为 0.15 度(0.15*111 公里)。
Things that I tried and didn't work
我尝试过但没有奏效的事情
The documentation doesn't tell where MKMapView
is getting its updates from. I tried
文档没有说明从哪里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]]);
}
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.
我对每个都有不同的价值。它看起来好像MKMapView
使用自己的CLLocationManager
,这意味着您无法设置其准确性。你不能加入你委托的CLLocationManager
的MKMapView
任。
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.
我的印象是设置精度的唯一方法是将显示用户位置设置为 NO 并创建一个带有蓝点的自定义注释,这意味着在发布时手动重新定位地图。您可以使用 github 项目图稿提取器从 SDK 中获取蓝点图形。
I don't know if I'm missing something or this part of MKMapView
just sucks.
我不知道是我遗漏了什么还是这部分MKMapView
很烂。
回答by morroko
For displaying map here is an example code.
在这里显示地图是一个示例代码。
First import MKMapKit and CoreLocation framework in your .h file.
首先在 .h 文件中导入 MKMapKit 和 CoreLocation 框架。
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
Add MKMapKit and CoreLocation Delegate in .h file
在 .h 文件中添加 MKMapKit 和 CoreLocation Delegate
@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];
Use CLLocationManager
for fetching user location.
使用CLLocationManager
用于获取用户的位置。
Declare an instance of CLLocationManager
声明一个 CLLocationManager 实例
CLLocationManager *locationManager;
In ViewDidLoad
在 ViewDidLoad
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManger startUpdatingLocation];
startUpdatingLocation
Method Implementation:
startUpdatingLocation
方法实现:
(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//Your Stuff
}
回答by nonamelive
You can't set the accuracy, however, you can get the accuracy via the userLocation in the MKMapView delegate method.
您无法设置精度,但是,您可以通过 MKMapView 委托方法中的 userLocation 获取精度。
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(@"%f", userLocation.location.horizontalAccuracy);
}