ios--定位
Core Location---定位框架
CLLocationManager这个属性必须写成全局的,不然初始化后就释放了。
授权
NSLocationWhenInUseUsageDescription :当app在前台的时候,才可以获取到定位信息 NSLocationAlwaysUsageDescription :app在前台、后台、挂起、结束进程状态时,都可以获取到定位信息 [locationManager requestWhenInUseAuthorization];//相对应info.plist中的NSLocationWhenInUseUsageDescription键 [locationManager requestAlwaysAuthorization];//相对应info.plist中的NSLocationAlwaysUsageDescription键
根据实际的情况进行控制。
实际授权的代码:
//以此来判断,是否是ios8 if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [locationManager requestWhenInUseAuthorization]; }kCLAuthorizationStatusNotDetermined:用户还没有被请求获取授权授权框只会弹出一次,如果客户关闭了定位的功能,应该弹出提示框。
kCLAuthorizationStatusRestricted:用户在设置里关闭了位置服务
kCLAuthorizationStatusDenied:用户收到获取授权的请求,但点击了NO,或者在设置里关闭了
kCLAuthorizationStatusAuthorized:用户收到获取授权的请求,点击了YES;(此状态在ios8废弃了,ios7以及以下可用)
kCLAuthorizationStatusAuthorizedAlways = kCLAuthorizationStatusAuthorized用户授权app在任何时候获取位置信息
kCLAuthorizationStatusAuthorizedWhenInUse:用户授权app在前台获取位置信息
if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied){
NSLog(@"请打开定位服务");
NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:settingsURL];
}
[self.locationManager startUpdatingLocation];开始定位服务 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations对应的代理方法
[_locationManager stopUpdatingLocation];
//定位会耗费大量的资源,所以在代理里面关闭定位。
现在需求:根据定位获取对应的地名
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
//NSLog(@"%@",locations.lastObject);
CLLocation *location=locations.lastObject;
[self.geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark=placemarks.lastObject;
NSLog(@"详细信息:%@",placemark.addressDictionary[@"FormattedAddressLines"]);
}];
}

浙公网安备 33010602011771号