地图定位相关的知识点补漏

作者: shaneZhang 分类: ios技术 发布时间: 2015-08-25 17:27
  • 检查地图定位是否开启权限

   if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
      [ZCUtility showNaviHelp:self];
   }
  • 应用从后台开启定位后应该重新定位并重新激活地图的定位模式

self.mapView.showsUserLocation = YES;
  • 监听系统从后台进入前台模式

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationActive) name:UIApplicationDidBecomeActiveNotification  object:nil];
  • 刷新app定位规则代码

- (void)refreshLocation
{

    self.isEnable = NO;
    if (!self.locationManager) {
        if ([CLLocationManager locationServicesEnabled]) {
            // if location services are restricted do nothing
            if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted) {
//              UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Determining your current location cannot be performed at this time because location services are enabled but restricted." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
//              [alertView show];
            } else {

                self.locationManager = [[CLLocationManager alloc] init];
                self.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
                [self.locationManager setDistanceFilter:1000.0f];
                self.locationManager.pausesLocationUpdatesAutomatically = NO;

                if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
                    [self.locationManager performSelector:@selector(requestWhenInUseAuthorization) withObject:nil];
                }
                
                self.isEnable = YES;
            }
        } else {
//            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Determining your current location cannot be performed at this time because location services are not enabled." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
//            [alertView show];
        }
    }

    if (self.isEnable) {
        self.locationManager.delegate = self;
        [self.locationManager startUpdatingLocation];

    } else {
        //NSLog(@"Location services is not enabled");
    }
}

如果觉得我的文章对您有用,请随意打赏。如果有其他问题请联系博主QQ(909491009)或者下方留言!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注