iOS之获取经纬度并通过反向地理编码获取详细地址

Posted 路很长

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS之获取经纬度并通过反向地理编码获取详细地址相关的知识,希望对你有一定的参考价值。

 1 _locationManager = [[CLLocationManager alloc] init];
 2 
 3     //期望的经度
 4 
 5     _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
 6 
 7     //大约变化100米更新一次
 8 
 9     _locationManager.distanceFilter = 100;
10 
11     //认证NSLocationAlwaysUsageDescription
12 
13     if ([[UIDevice currentDevice] systemVersion].doubleValue > 8.0) {//如果ios是8.0以上版本
14 
15         if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {//位置管理对象中有requestAlwaysAuthorization这个方法
16 
17             //运行
18 
19             [_locationManager requestAlwaysAuthorization];
20 
21         }
22 
23     }
24 
25     _locationManager.delegate = self;
26 
27     [_locationManager startUpdatingLocation];
28 
29  
30 
31 //获取经纬度和详细地址
32 
33 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
34 
35     
36 
37     CLLocation *location = [locations lastObject];
38 
39     NSLog(@"latitude === %g  longitude === %g",location.coordinate.latitude, location.coordinate.longitude);
40 
41     
42 
43     //反向地理编码
44 
45     CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init];
46 
47     CLLocation *cl = [[CLLocation alloc] initWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude];
48 
49     [clGeoCoder reverseGeocodeLocation:cl completionHandler: ^(NSArray *placemarks,NSError *error) {
50 
51         for (CLPlacemark *placeMark in placemarks) {
52 
53             
54 
55             NSDictionary *addressDic = placeMark.addressDictionary;
56 
57             
58 
59             NSString *state=[addressDic objectForKey:@"State"];
60 
61             NSString *city=[addressDic objectForKey:@"City"];
62 
63             NSString *subLocality=[addressDic objectForKey:@"SubLocality"];
64 
65             NSString *street=[addressDic objectForKey:@"Street"];
66 
67             
68 
69             NSLog(@"所在城市====%@ %@ %@ %@", state, city, subLocality, street);
70 
71             [_locationManager stopUpdatingLocation];
72 
73         }
74 
75     }];
76 
77 }
78 
79  

 

以上是关于iOS之获取经纬度并通过反向地理编码获取详细地址的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iOS 中使用反向谷歌地理编码?

(七十七)地理编码与反地理编码

iOS定位的使用:地理/逆地理编码/判断目标经纬度是否在大陆

猫猫学iOS 之CoreLocation反地理编码小Demo输入经纬度得到城市

如何通过经纬度坐标获取附近的地址信息?

使用前向地理编码获取iOS中许多地址的纬度和经度时如何添加延迟?