定位CoreLocation

//添加包:CoreLocation.framework
//.plist文件: 请求获得应用一直使用定位服务授权,注意使用此方法前要在info.plist中配置 NSLocationAlwaysUsageDescription; 请求获得应用使用时的定位服务授权,注意使用此方法前在要在info.plist中配置 NSLocationWhenInUseUsageDescription,都设置为YES
手动输入

@property ( nonatomic , retain ) CLLocationManager *locationManager;
// 地理编码 / 逆地理编码
@property ( nonatomic , retain ) CLGeocoder *geocoder;

- ( void )createLocationManager
{
   
self . locationManager = [[ CLLocationManager alloc ] init ];
   
self . geocoder = [[ CLGeocoder alloc ] init ];
   
   
if (![ CLLocationManager locationServicesEnabled ]) {
       
NSLog ( @" 定位服务当前可能尚未打开 , 请设置打开 " );
       
return ;
    }
   
// 如果没有授权则请求用户授权
   
if ([ CLLocationManager authorizationStatus ] == kCLAuthorizationStatusNotDetermined ) {
        [
self . locationManager requestWhenInUseAuthorization ];
    }
else if ([ CLLocationManager authorizationStatus ] == kCLAuthorizationStatusAuthorizedWhenInUse ) {
       
// 设置代理
       
self . locationManager . delegate = self ;
       
// 设置定位精度
       
self . locationManager . desiredAccuracy = kCLLocationAccuracyBest ;
       
// 定位频率 , 每隔多少米定位一次
//        CLLocationDistance distance = 1000.0;// 十米定位一次
       
self . locationManager . distanceFilter = kCLDistanceFilterNone ;
       
// 启动跟踪定位
        [
self . locationManager startUpdatingLocation ];
    }
 
}

#pragma 跟踪定位代理方法 , 每次位置发生变化即执行
- ( void )locationManager:( CLLocationManager *)manager didUpdateLocations:( NSArray < CLLocation *> *)locations
{
   
CLLocation *location=[locations firstObject ]; // 取出第一个位置
   
CLLocationCoordinate2D coordinate=location. coordinate ; // 位置坐标
    NSLog(@"经度:%f,纬度:%f,海拔:%f,航向:%f,行走速度:%f",coordinate.longitude,coordinate.latitude,location.altitude,location.course,location.speed);
   
   [ self getAddressByLatitude:coordinate.latitude longitude:coordinate.longitude];
    // 如果不需要实时定位,使用完即使关闭定位服务
    [
_locationManager stopUpdatingLocation ];
}
// CLGeocoder类用于处理地理编码和逆地理编码(又叫反地理编码)功能.
地理编码:根据给定的位置(通常是地名)确定地理坐标(经、纬度)。
逆地理编码:可以根据地理坐标(经、纬度)确定位置信息(街道、门牌等
#pragma mark 根据坐标取得地名(逆地理编码)
-( void )getAddressByLatitude:( CLLocationDegrees )latitude longitude:( CLLocationDegrees )longitude{
   
// 反地理编码
   
CLLocation *location=[[ CLLocation alloc ] initWithLatitude :latitude longitude :longitude];
    [
_geocoder reverseGeocodeLocation :location completionHandler :^( NSArray *placemarks, NSError *error) {
       
CLPlacemark *placemark=[placemarks firstObject ];
        NSLog(@"%@", placemark.name);//位置名
        NSLog ( @"thoroughfare,%@" ,place. thoroughfare );       // 街道
        NSLog ( @"subThoroughfare,%@" ,place. subThoroughfare ); // 子街道
       
NSLog ( @"locality,%@" ,place. locality );               //
       
NSLog ( @"subLocality,%@" ,place. subLocality );         //
        NSLog(@"country,%@",place.country);                 // 国家
        // 字典形式输出
//        NSLog(@" 详细信息 :%@",placemark.addressDictionary);
    }];
}
//
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值