您的位置:首页 > 大数据 > 人工智能

使用苹果地图定位地理信息反编码 遇到 Error Domain=kCLErrorDomain Code=8 问题

2016-04-20 14:13 686 查看
在IOS8中定位功能新增了两个方法:

//使用程序其间允许访问位置数据(iOS8定位需要)


- (void)requestWhenInUseAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);

 [b]//获得授权认证
[/b]

- (void)requestAlwaysAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0) __TVOS_PROHIBITED;

这两个方法使定位功能无法正常使用

若想正常使用 在Info.plist中加入两个缺省没有的字段

NSLocationAlwaysUsageDescription

NSLocationWhenInUseUsageDescription

这两句是自定义提示用户授权使用定位功能时的提示语。(可以将提示的其他信息写在字段的后边,不写也可以)

    _locationManager=[[CLLocationManager alloc] init];

    _locationManager.delegate=self;

    _locationManager.desiredAccuracy=kCLLocationAccuracyBest;

    _locationManager.distanceFilter=10;

    [_locationManager startUpdatingLocation];//开启定位

 

    if ([[[UIDevice currentDevice] systemVersion] floatValue]  >= 8.0) {

        [_locationManager requestWhenInUseAuthorization];//使用程序其间允许访问位置数据(iOS8定位需要)

//        获得授权认证

        [_locationManager requestAlwaysAuthorization];

    }

我在页面返回的时候结束更新

- (void)back{

    //停止更新位置(如果定位服务不需要实时更新的话,那么应该立即停止位置的更新)

    [_locationManager stopUpdatingLocation];

    

    [self.navigationController popViewControllerAnimated:YES];

    

}
#pragma mark-CLLocationManagerDelegate

/**

 *  当定位到用户的位置时,就会调用(调用的频率比较频繁)

 */

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

 //locations数组里边存放的是CLLocation对象,一个CLLocation对象就代表着一个位置

    CLLocation *loc = [locations firstObject];

    longitude = [NSString stringWithFormat:@"%f",loc.coordinate.longitude];

    latitude = [NSString stringWithFormat:@"%f",loc.coordinate.latitude];

    NSLog(@"纬度=%@,经度=%@",latitude,longitude);

//得到经纬度以后根据经纬度获取所在的省市区地址

//创建位置

    CLGeocoder *revGeo = [[CLGeocoder alloc] init];

//在此时遇到了Error Domain=kCLErrorDomain Code=8 问题 这个是因为参数 错误所以要注意如果有这个问题的看你的参数错误没有
应该为(manager.location

    [revGeo reverseGeocodeLocation:manager.location completionHandler:^(NSArray *placemarks, NSError *error) {

      if (placemarks.count > 0) {

            CLPlacemark *placemark = placemarks[0];

            NSDictionary *dic = [placemark addressDictionary];

            NSLog(@"dic %@",dic);//根据你的需要选取所需要的地址

            //城市要注意

            NSString *city = placemark.locality;

            if (!city) {

                // 四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)

                city = placemark.administrativeArea;

            }

            NSLog(@"city :%@",city);

        }

        else if (error == nil && [placemarks count] == 0)

        {

            NSLog(@"No results were returned.");

        }

        else if (error != nil)

        {

            NSLog(@"An error occurred = %@", error);

        }

    }];

    //系统会一直更新数据,直到选择停止更新,因为我只需要获得一次经纬度即可,所以获取之后就停止更新

    [manager stopUpdatingLocation];

}

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: