ios 定位和反地理编码

现在定位sdk比较多 ,但是如果是简单的定位的话,还是用苹果自带的方法比较简单。如果想了解百度sdk定位 点击了解百度定位(含demo) 本文 主要讲解苹果自带的定位CLLocationManager
1) 首先 在info.plist 文件中添加在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<string>app需要您的同意,才能访问位置</string>
</plist>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<string>app需要您的同意,才能访问位置</string>
</plist>

2) 在appdelegate 中引入

  #import <CoreLocation/CoreLocation.h>

3) 声明属性 引入代理

@interface AppDelegate ()<CLLocationManagerDelegate>
{
    CLLocationManager *_locationManager;
}
@end

4) 初始化CLLocationManager,实现代理方法

- (void)loadLocation{
    //定位管理器
    _locationManager=[[CLLocationManager alloc]init];
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"定位服务当前可能尚未打开,请设置打开!");
        return;
    }
    //如果没有授权则请求用户授权
    if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined){
        [_locationManager requestWhenInUseAuthorization];
        NSLog(@"定位服务打开!");
        [self startLocation];
    }else{
        [_locationManager requestWhenInUseAuthorization];
        [self startLocation];
    }
}

- (void)startLocation{
    //定位管理器
    //设置代理
    _locationManager.delegate=self;
    //设置定位精度
    _locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    //定位频率,每隔多少米定位一次
    CLLocationDistance distance=10.0;//十米定位一次
    _locationManager.distanceFilter=distance;
    //启动跟踪定位
    [_locationManager startUpdatingLocation];
}

#pragma mark - CoreLocation 代理
#pragma mark 跟踪定位代理方法,每次位置发生变化即会执行(只要定位到相应位置)
//可以通过模拟器设置一个虚拟位置,否则在模拟器中无法调用此方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)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 reverseGeocoder:location];
}

#pragma mark Geocoder
//反地理编码
- (void)reverseGeocoder:(CLLocation *)currentLocation {
    CLGeocoder* geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        if(error || placemarks.count == 0){
            NSLog(@"error = %@",error);
        }else{
            CLPlacemark* placemark = placemarks.firstObject;
            //City 是获取城市的key
            NSString * city = [[placemark addressDictionary] objectForKey:@"City"];
            //SubLocality 是获取详细地址的key
            NSString * country = [[placemark addressDictionary] objectForKey:@"SubLocality"];
            //State 是获取省的key
            NSString * provice = [[placemark addressDictionary] objectForKey:@"State"];
            NSString * moreAddress = [[placemark addressDictionary] objectForKey:@"FormattedAddressLines"][0];
            NSLog(@"city:%@,country:%@,provice:%@,moreAddress:%@",city,country,provice,moreAddress);

        }
    }];
}

5) 在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中调用

  [self loadLocation];

6) 运行 查看效果
在这里插入图片描述
demo链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值