MKMapView 定位
进入前台可以使用 MKMapView 定位并画线
mapView.mapType = MKMapTypeStandard;
mapView.delegate = self;
//表示用定位
mapView.showsUserLocation = YES;
代理
//通过这个更新用户定位数据
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
//画线
- (MKPolyline *)polylineForLocations:(NSArray *)locations {
CLLocationCoordinate2D coordinates[locations.count];
for (int i = 0; i < locations.count; i++) {
CLLocation *location = locations[i];
coordinates[i] = location.coordinate;
}
return [MKPolyline polylineWithCoordinates:coordinates count:locations.count];
}
但是进入后台定位后,MKMapView就不起作用了,必须用到CLLocationManager
后台定位 CLLocationManager
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
if ([UIDevice currentDevice].systemVersion.floatValue >= 9.0) {
//允许后台定位
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
}
// 精确度最佳
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;//导航级别的精确度
//位置管理器专门为其他类型的大型交通工具导航,而不是汽车导航 CLActivityTypeOtherNavigation
////位置管理器专门为步行、跑步、骑自行车等健身时,提供导航
self.locationManager.activityType = CLActivityTypeFitness;
self.locationManager.pausesLocationUpdatesAutomatically = NO; //不允许自动暂停刷新
self.locationManager.distanceFilter = kCLDistanceFilterNone; //不需要移动都可以刷新
代理
//用户定位获取, 这里有个坑,如果是国内的App就是这里的定位信息需要转成国内,不然定位信息对不上
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
+(CLLocationCoordinate2D)transformFromWGSToGCJ:(CLLocationCoordinate2D)wgsLoc
{
CLLocationCoordinate2D adjustLoc;
// if([self isLocationOutOfChina:wgsLoc]){
// adjustLoc = wgsLoc;
// }else{
double adjustLat = [self transformLatWithX:wgsLoc.longitude - 105.0 withY:wgsLoc.latitude - 35.0];
double adjustLon = [self transformLonWithX:wgsLoc.longitude - 105.0 withY:wgsLoc.latitude - 35.0];
double radLat = wgsLoc.latitude / 180.0 * pi;
double magic = sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = sqrt(magic);
adjustLat = (adjustLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
adjustLon = (adjustLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
adjustLoc.latitude = wgsLoc.latitude + adjustLat;
adjustLoc.longitude = wgsLoc.longitude + adjustLon;
// }
return adjustLoc;
}
//获取授权弹框处理逻辑
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
switch (status) {
case kCLAuthorizationStatusNotDetermined:
// 请求定位权限
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
break;
case kCLAuthorizationStatusRestricted:
case kCLAuthorizationStatusDenied:
if (self.locationBlock) {
self.locationBlock(NO);
}
break;
case kCLAuthorizationStatusAuthorizedAlways:
case kCLAuthorizationStatusAuthorizedWhenInUse:
if (self.locationBlock) {
self.locationBlock(YES);
}
break;
default:
if (self.locationBlock) {
self.locationBlock(NO);
}
break;
}
}
后台定位后,进入前台需要将后台定位信息画上去
这里用到监听切换前后台通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
-(void)applicationWillEnterForeground:(NSNotification *)noti {
NSLog(@"进入前台");
if (self.isSportMap) {
mapView.showsUserLocation = YES;
[self.locationManager stopUpdatingLocation];
[self drawRoute];
}
}
-(void)applicationDidEnterBackground:(NSNotification *)noti{
NSLog(@"进入后台");
if (self.isSportMap) {
mapView.showsUserLocation = NO;
[self.locationManager requestAlwaysAuthorization];
[self.locationManager startUpdatingLocation];
}
}