设为首页 加入收藏

TOP

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

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇NSTimer 定时器总结 下一篇HTTP和GET/POST请求(NSURLConnect..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目