您的位置:首页 > 移动开发 > IOS开发

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);

   

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