iOS GPS定位CLLocationManager获取当前位置坐标 示例代码

参考地址: 

http://www.jianshu.com/p/ef6994767cbb

http://www.jianshu.com/p/f58be9373b6a


- (void)initLocationManager {
    if ([CLLocationManager locationServicesEnabled]) {
        if (self.locationManager == nil){
            self.locationManager = [[CLLocationManager alloc] init];
        }
        self.locationManager.delegate = self;
        self.locationManager.distanceFilter = 1.0;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        
        if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
        {
            [self.locationManager requestAlwaysAuthorization]; // 永久授权
            [self.locationManager requestWhenInUseAuthorization]; //使用中授权
        }
        
        [self.locationManager startUpdatingLocation];//开启位置更新
    }
}


// 实现代理方法 
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    CLLocation * currentLocation = [locations lastObject];
    if (currentLocation != nil) {
        [self.locationManager stopUpdatingLocation];             // 停止定位
    }
    double altitude = currentLocation.altitude;                 // 高度
    double latitude = currentLocation.coordinate.latitude;
    double longitude = currentLocation.coordinate.longitude;
    
    NSLog(@"altitude:%f, latitude: %f, longitude:%f", altitude, latitude, longitude)
    
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"%@", error);
    if (error.code == kCLErrorDenied) {
        NSLog(@"%@", error);
    }
}


ios8中需要配置键  info.plist 中加入以下键值

<key>NSLocationWhenInUseUsageDescription</key>
<string>请允许GPS定位,获取更好的体验效果</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>请允许GPS定位,获取更好的体验效果</string> 

key的数据类型据测试结果好像string(一段字符串描述) 和 bool( YES)类型 都可以定位的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风流 少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值