Objective-C iOS 根据经纬度获取地名,根据地名获取经纬度(地理编码与反编码)

2017 年 1 月 6 日 0 条评论 2.46k 次阅读 0 人点赞

网上看了很多,各种问题,各种错误。下面自己写的留档一下。

引入头文件

#import <CoreLocation/CoreLocation.h>

创建几个变量


//创建一个定位管理者

@property (nonatomic ,strong) CLLocationManager *mgr;

//经纬度

@property(readonly, nonatomic) CLLocationCoordinate2D coordinate;

//海拔

@property(readonly, nonatomic) CLLocationDistance altitude;

//路线,航向(取值范围是0.0° ~ 359.9°,0.0°代表真北方向)

@property(readonly, nonatomic) CLLocationDirection course;

//行走速度(单位是m/s)

@property(readonly, nonatomic) CLLocationSpeed speed;

//地理编码对象

@property (nonatomic ,strong) CLGeocoder *geocoder;

 

viewDidLoad里面引用


- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

&nbsp;

//1.设定CoreLocation管理者的代理监听获取到的位置

self.mgr.delegate = self;

// 判断是否是iOS8

if([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0)

{

NSLog(@"是iOS8");

// 主动要求用户对我们的程序授权, 授权状态改变就会通知代理

[self.mgr requestAlwaysAuthorization]; // 请求前台和后台定位权限

}else

{

NSLog(@"是iOS7");

// 3.开始监听(开始获取位置)

[self.mgr startUpdatingLocation];

}

}

方法


/**

*  授权状态发生改变时调用

*

*  @param manager 触发事件的对象

*  @param status  当前授权的状态

*/

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

{

switch (status) {

case kCLAuthorizationStatusAuthorizedAlways:

NSLog(@"持续使用授权");

[self.mgr startUpdatingLocation];

break;

case kCLAuthorizationStatusAuthorizedWhenInUse:

NSLog(@"使用中授权");

[self.mgr startUpdatingLocation];

break;

case kCLAuthorizationStatusDenied:

NSLog(@"授权被拒绝");

break;

case kCLAuthorizationStatusNotDetermined:

NSLog(@"等待用户授权");

break;

default:

NSLog(@"授权失败");

break;

}

&nbsp;

}

/**

*  获取到位置信息之后就会调用(调用频率非常高)

*

*  @param manager   触发事件的对象

*  @param locations 获取到的位置

*/

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{

NSLog(@"%s", __func__);

// 如果只需要获取一次, 可以获取到位置之后就停止

//    [self.mgr stopUpdatingLocation];

&nbsp;

// 1.获取最后一次的位置

/*

location.coordinate; 坐标, 包含经纬度

location.altitude; 设备海拔高度 单位是米

location.course; 设置前进方向 0表示北 90东 180南 270西

location.horizontalAccuracy; 水平精准度

location.verticalAccuracy; 垂直精准度

location.timestamp; 定位信息返回的时间

location.speed; 设备移动速度 单位是米/秒, 适用于行车速度而不太适用于不行

*/

/*

可以设置模拟器模拟速度

bicycle ride 骑车移动

run 跑动

freeway drive 高速公路驾车

*/

CLLocation *location = [locations lastObject];

NSLog(@"目前系统位置是:纬度 = %f, 经度 = %f 速度 = %f", location.coordinate.latitude , location.coordinate.longitude, location.speed);

&nbsp;

[self geocodeBtnClick:@"北京市"];

NSString *strLatitude = [NSString stringWithFormat:@"%f",location.coordinate.latitude];

NSString *strLongitude = [NSString stringWithFormat:@"%f",location.coordinate.longitude];

[self addRessGeocode:strLatitude longtitude:strLongitude];

//[self addRessGeocode:@"29" longtitude:@"121"];

}

#pragma mark - 懒加载

- (CLLocationManager *)mgr

{

if (!_mgr) {

_mgr = [[CLLocationManager alloc] init];

}

return _mgr;

}

//地理编码

- (void)geocodeBtnClick:(NSString*) addStr

{

// 0.获取用户输入的位置

NSString *addressStr = addStr;

if (addressStr == nil || addressStr.length == 0) {

NSLog(@"请输入地址");

return;

}

&nbsp;

&nbsp;

// 1.创建地理编码对象

//创建地理编码对象

CLGeocoder *geocoder=[[CLGeocoder alloc]init];

// 2.利用地理编码对象编码

// 根据传入的地址获取该地址对应的经纬度信息

[geocoder geocodeAddressString:addressStr completionHandler:^(NSArray *placemarks, NSError *error) {

&nbsp;

if (placemarks.count == 0 || error != nil) {

return ;

}

// placemarks地标数组, 地标数组中存放着地标, 每一个地标包含了该位置的经纬度以及城市/区域/国家代码/邮编等等...

// 获取数组中的第一个地标

CLPlacemark *placemark = [placemarks firstObject];

//        for (CLPlacemark  *placemark in placemarks) {

//            NSLog(@"%@ %@ %f %f", placemark.name, placemark.addressDictionary, placemark.location.coordinate.latitude, placemark.location.coordinate.longitude);

&nbsp;

NSLog(@"%@经纬度分别是:,维度:%f,经度:%f",addStr,placemark.location.coordinate.latitude,placemark.location.coordinate.longitude);

&nbsp;

&nbsp;

}];

}

//反地理编码

- (void)addRessGeocode:(NSString *)latitude longtitude:(NSString *)longtitude

{

NSLog(@"反地理编码 传入的值:%@,%@",latitude,longtitude);

&nbsp;

//创建地理编码对象

CLGeocoder *geocoder=[[CLGeocoder alloc]init];

//创建位置

CLLocation *location=[[CLLocation alloc]initWithLatitude:[latitude floatValue] longitude:[longtitude floatValue]];

[geocoder reverseGeocodeLocation:location completionHandler: ^(NSArray *placemarks,NSError *error) {

for (CLPlacemark *placeMark in placemarks)

{

NSDictionary *addressDic=placeMark.addressDictionary;

&nbsp;

NSString *state=[addressDic objectForKey:@"State"];

NSString *city=[addressDic objectForKey:@"City"];

NSString *subLocality=[addressDic objectForKey:@"SubLocality"];

NSString *street=[addressDic objectForKey:@"Street"];

&nbsp;

NSLog(@"CC = %@,%@,%@,%@",state,city,subLocality,street);

&nbsp;

//[self stopLocation];

//[_chooseCityBtn setTitle:city forState:UIControlStateNormal];

//[_activityIndicator stopAnimating];

}

&nbsp;

}];

}

//#pragma mark - 懒加载

- (CLGeocoder *)geocoder

{

if (!_geocoder) {

_geocoder = [[CLGeocoder alloc] init];

}

return _geocoder;

}

&nbsp;

&nbsp;

 

最后,Info.plist 文件增加2个键值

Privacy - Location Always Usage Description     string     系统想要使用您的位置!
Privacy - Location Usage Description     string     系统想要使用您的位置!

雷雷

这个人太懒什么东西都没留下

文章评论(0)

(Spamcheck Enabled)