iOS9.1 判断定位是否可用 并定位自己位置
2016-01-29 11:59
716 查看
由于iOS9.1关闭整个系统定位会在调用定位时直接出提示,所以没有做判断 ,这个只是在判断应用定位是否可用,以及定位不到的情况!!
你需要在info.plist表里面添加两条变量
在Info.plist中加入两个缺省没有的字段
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
这两个字段没什么特别的意思,就是自定义提示用户授权使用地理定位功能时的提示语。
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<CLLocationManagerDelegate>
{
CLLocationDegrees lati;
CLLocationDegrees longti;
}
/**
* 地图
*/
@property (nonatomic, strong)CLLocationManager *cllocationManager;
// 设置地图启用定位
- (void)setMap {
//定位功能可用,开始定位
_cllocationManager = [[CLLocationManager alloc]init];
_cllocationManager.delegate = self;
_cllocationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[_cllocationManager requestWhenInUseAuthorization];
[_cllocationManager startUpdatingLocation];
}
// 判断定位是否可用
- (void)locationManager: (CLLocationManager *)manager didFailWithError: (NSError *)error {
NSString *errorString;
[manager stopUpdatingLocation];
NSLog(@"Error: %@",[error localizedDescription]);
switch([error code]) {
case kCLErrorDenied:
//Access denied by user
errorString = @"用户关闭";
// 定位不可用 —— 传虚拟经纬度
lati = 0.000000;
longti = 0.000000;
//Do something...
break;
case kCLErrorLocationUnknown:
//Probably temporary...
errorString = @"位置数据不可用";
//Do something else...
break;
default:
errorString = @"未知错误";
break;
}
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:errorString delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
// 代理方法 地理位置反编码
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
NSLog(@"5");
CLLocation *newlocation = locations[0];
CLLocationCoordinate2D oCoordinate = newlocation.coordinate;
NSLog(@"经度:%f,维度:%f",oCoordinate.longitude,oCoordinate.latitude);
// 给经纬度全局属性赋值
lati = oCoordinate.latitude;
longti = oCoordinate.longitude;
// [NSTimer scheduledTimerWithTimeInterval:8.0 target:self selector:@selector(action:) userInfo:nil repeats:nil];
[_cllocationManager stopUpdatingLocation];
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:newlocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
for (CLPlacemark *place in placemarks) {
NSDictionary *location =[place addressDictionary];
NSLog(@"国家:%@",[location objectForKey:@"Country"]);
NSLog(@"城市:%@",[location objectForKey:@"State"]);
NSLog(@"区:%@",[location objectForKey:@"SubLocality"]);
NSLog(@"位置:%@", place.name);
NSLog(@"国家:%@", place.country);
NSLog(@"城市:%@", place.locality);
NSLog(@"区:%@", place.subLocality);
NSLog(@"街道:%@", place.thoroughfare);
NSLog(@"子街道:%@", place.subThoroughfare);
}
}];
NSLog(@"@@@@@@@@@@=====%f,%f",lati,longti);
}
你需要在info.plist表里面添加两条变量
在Info.plist中加入两个缺省没有的字段
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
这两个字段没什么特别的意思,就是自定义提示用户授权使用地理定位功能时的提示语。
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<CLLocationManagerDelegate>
{
CLLocationDegrees lati;
CLLocationDegrees longti;
}
/**
* 地图
*/
@property (nonatomic, strong)CLLocationManager *cllocationManager;
// 设置地图启用定位
- (void)setMap {
//定位功能可用,开始定位
_cllocationManager = [[CLLocationManager alloc]init];
_cllocationManager.delegate = self;
_cllocationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[_cllocationManager requestWhenInUseAuthorization];
[_cllocationManager startUpdatingLocation];
}
// 判断定位是否可用
- (void)locationManager: (CLLocationManager *)manager didFailWithError: (NSError *)error {
NSString *errorString;
[manager stopUpdatingLocation];
NSLog(@"Error: %@",[error localizedDescription]);
switch([error code]) {
case kCLErrorDenied:
//Access denied by user
errorString = @"用户关闭";
// 定位不可用 —— 传虚拟经纬度
lati = 0.000000;
longti = 0.000000;
//Do something...
break;
case kCLErrorLocationUnknown:
//Probably temporary...
errorString = @"位置数据不可用";
//Do something else...
break;
default:
errorString = @"未知错误";
break;
}
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:errorString delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
// 代理方法 地理位置反编码
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
NSLog(@"5");
CLLocation *newlocation = locations[0];
CLLocationCoordinate2D oCoordinate = newlocation.coordinate;
NSLog(@"经度:%f,维度:%f",oCoordinate.longitude,oCoordinate.latitude);
// 给经纬度全局属性赋值
lati = oCoordinate.latitude;
longti = oCoordinate.longitude;
// [NSTimer scheduledTimerWithTimeInterval:8.0 target:self selector:@selector(action:) userInfo:nil repeats:nil];
[_cllocationManager stopUpdatingLocation];
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:newlocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
for (CLPlacemark *place in placemarks) {
NSDictionary *location =[place addressDictionary];
NSLog(@"国家:%@",[location objectForKey:@"Country"]);
NSLog(@"城市:%@",[location objectForKey:@"State"]);
NSLog(@"区:%@",[location objectForKey:@"SubLocality"]);
NSLog(@"位置:%@", place.name);
NSLog(@"国家:%@", place.country);
NSLog(@"城市:%@", place.locality);
NSLog(@"区:%@", place.subLocality);
NSLog(@"街道:%@", place.thoroughfare);
NSLog(@"子街道:%@", place.subThoroughfare);
}
}];
NSLog(@"@@@@@@@@@@=====%f,%f",lati,longti);
}
相关文章推荐
- iOS开发——Masonry 自动布局
- IOS 后台挂起程序 当程序到后台后,继续完成Long-Running Task 任务
- 【iOS开发】---- 手把手教你github托管代码
- iOS 常用技术 收藏
- iOS 获取WIFI SSID及MAC地址
- iOS获取设备信息和获取当前屏幕状态
- 修复iOS模拟器无法启动
- IOS 开发环境,证书和授权文件等详解
- iOS开发之跳转至设置页面小功能
- iOS-点击按钮实现复制功能
- iOS 乱码问题之一
- MBProgressHUD使用介绍
- AFNetworking上传图片
- iOS开发BaseModel的创建和使用(基类)
- iOS加速审核
- iOS 中三种定时器的用法NSTimer、CADisplayLink、GCD
- 手势的3个容易混淆的属性 cancelsTouchesInView/delaysTouchesBegan/delaysTouchesEnded
- iOS 去掉tabbar顶部的横线
- iOS 单例复用性最强的写法
- iOS 禁止横屏的解决方案