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  
原文地址:https://www.cnblogs.com/feiyiban588/p/5596477.html