iOS8中使用CoreLocation定位
2015-02-12 14:21
225 查看
在iOS8中,苹果已经强制开发者在请求定位服务时获得用户的授权,此外iOS状态栏中还有指示图标,提示用户当前应用是否正在使用定位服务。另外在iOS8中,苹果进一步改善了定位服务,让开发者请求定位服务时需要向用户提供更多的透明。此外,iOS8中还支持让应用开发者调用全新的“访问监控”功能,当用户允许后应用才能获得更多的定位数据。
2、初始化CLLocationManager并开始定位
3、实现CLLocationManagerDelegate的代理方法
(1)获取到位置数据,返回的是一个CLLocation的数组,一般使用其中的一个
(2)获取用户位置数据失败的回调方法,在此通知用户
4、在
iOS8对定位进行了一些修改,其中包括定位授权的方法,CLLocationManager增加了下面的两个方法:
(1)始终允许访问位置信息
(2)使用应用程序期间允许访问位置数据
示例如下:
2、在Info.plist文件中添加如下配置:
(1)
(2)
这两个键的值就是授权alert的描述,示例配置如下[
2、《IOS开发之Core Location》
3、《IOS8下的定位授权》
//定位代理经纬度回调
-(void)locationManager:(CLLocationManager *)managerdidUpdateToLocation:(CLLocation*)newLocation
fromLocation:(CLLocation *)oldLocation {
[locationManagerstopUpdatingLocation];
NSLog(@"location ok");
NSLog(@"%@",[NSStringstringWithFormat:@"经度:%3.5f\n纬度:%3.5f",newLocation.coordinate.latitude,newLocation.coordinate.longitude]);
CLGeocoder * geoCoder = [[CLGeocoder
alloc] init];
[geoCoder
reverseGeocodeLocation:newLocationcompletionHandler:^(NSArray*placemarks,
NSError *error){
for(CLPlacemark * placemarkin placemarks) {
NSDictionary *test = [placemarkaddressDictionary];
// Country(国家) State(城市) SubLocality(区)
NSLog(@"%@", [test
objectForKey:@"State"]);
}
}];
}
iOS8以前使用CoreLocation定位
1、首先定义一个全局的变量用来记录CLLocationManager对象,引入CoreLocation.framework使用
#import <CoreLocation/CoreLocation.h>
1 | @property (nonatomic, strong) CLLocationManager *locationManager; |
12 | self.locationManager = [[CLLocationManager alloc]init]; _locationManager.delegate = self; _locationManager.desiredAccuracy = kCLLocationAccuracyBest; _locationManager.distanceFilter = 10; [_locationManager startUpdatingLocation]; |
(1)获取到位置数据,返回的是一个CLLocation的数组,一般使用其中的一个
12 | - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation *currLocation = [locations lastObject]; NSLog(@"经度=%f 纬度=%f 高度=%f", currLocation.coordinate.latitude, currLocation.coordinate.longitude, currLocation.altitude); } |
12 | - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { if ([error code] == kCLErrorDenied) { //访问被拒绝 } if ([error code] == kCLErrorLocationUnknown) { //无法获取位置信息 } } |
viewWillDisappear关闭定位
12 | - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [_locationManager stopUpdatingLocation]; } |
iOS8中使用CoreLocation定位
1、在使用CoreLocation前需要调用如下函数【iOS8专用】:iOS8对定位进行了一些修改,其中包括定位授权的方法,CLLocationManager增加了下面的两个方法:
(1)始终允许访问位置信息
- (void)requestAlwaysAuthorization;
(2)使用应用程序期间允许访问位置数据
- (void)requestWhenInUseAuthorization;
示例如下:
12 | self.locationManager = [[CLLocationManager alloc]init]; _locationManager.delegate = self; _locationManager.desiredAccuracy = kCLLocationAccuracyBest; _locationManager.distanceFilter = 10; [_locationManager requestAlwaysAuthorization];//添加这句 [_locationManager startUpdatingLocation]; |
(1)
NSLocationAlwaysUsageDescription
(2)
NSLocationWhenInUseUsageDescription
这两个键的值就是授权alert的描述,示例配置如下[
勾选Show Raw Keys/Values后进行添加]:
参考资料
1、《迎接iOS8 – CoreLocation的变化》2、《IOS开发之Core Location》
3、《IOS8下的定位授权》
//定位代理经纬度回调
-(void)locationManager:(CLLocationManager *)managerdidUpdateToLocation:(CLLocation*)newLocation
fromLocation:(CLLocation *)oldLocation {
[locationManagerstopUpdatingLocation];
NSLog(@"location ok");
NSLog(@"%@",[NSStringstringWithFormat:@"经度:%3.5f\n纬度:%3.5f",newLocation.coordinate.latitude,newLocation.coordinate.longitude]);
CLGeocoder * geoCoder = [[CLGeocoder
alloc] init];
[geoCoder
reverseGeocodeLocation:newLocationcompletionHandler:^(NSArray*placemarks,
NSError *error){
for(CLPlacemark * placemarkin placemarks) {
NSDictionary *test = [placemarkaddressDictionary];
// Country(国家) State(城市) SubLocality(区)
NSLog(@"%@", [test
objectForKey:@"State"]);
}
}];
}
相关文章推荐
- iOS8中使用CoreLocation定位
- iOS8中使用CoreLocation定位
- iOS8中使用CoreLocation定位
- iOS8中使用CoreLocation定位
- iOS8中使用CoreLocation定位[转]
- iOS8中使用CoreLocation定位
- Swift - 使用CoreLocation实现定位(经纬度、海拔、速度、距离等)
- iOS8 中使用定位功能必须先获取用户授权,无论CLLocationManager还是MapKit
- IOS——地图的使用、地图定位到当前位置(包括ios8获取位置)、地图标注的添加、地图放大缩小监听
- IOS学习之——CoreLocation定位的使用
- iOS项目开发实战——使用CoreLocation实现定位
- iOS开发手记-iOS8中使用定位服务解决方案
- ios8之后使用CLLocationManager进行定位
- 疯狂ios讲义之使用CoreLocation定位(5)
- iOS8地图定位-CoreLocation
- IOS 使用CoreLocation定位 不弹出权限提示
- Swift - 使用CoreLocation实现定位(经纬度、海拔、速度、距离等)
- CoreLocation的使用 定位 既地理编码与反地理编码
- iOS8使用CLLocationManager定位时,代理方法不执行的解决方法
- [IOS]IOS8下地图定位的使用方法