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