if ([UIDevice currentDevice].systemVersion.doubleValue > 8.0) {
[self.locMgr requestAlwaysAuthorization];
} else {
[self.locMgr startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusNotDetermined) {
NSLog(@"等待用户授权");
}
if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways) {
// 得到用户授权开始定位
[self.locMgr startUpdatingLocation];
} else {
NSLog(@"授权失败");
}
}
/**
* 当定位到用户的位置时,就会调用(调用的频率比较频繁)
*/
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
// locations 数组里存放的是 CLLocation 对象, 一个 CLLocation 对象就代表着一个位置
CLLocation *loc = [locations firstObject];
// 纬度 : loc.coordinate.latitude
// 经度 : loc.coordinate.longitude
NSLog(@"纬度:%f, 经度:%f",loc.coordinate.latitude,loc.coordinate.longitude);
CLLocation *c = [[CLLocation alloc] initWithLatitude:loc.coordinate.latitude longitude:loc.coordinate.longitude];
//创建位置
CLGeocoder *revGeo = [[CLGeocoder alloc] init];
[revGeo reverseGeocodeLocation:c completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (!error && [placemarks count] > 0) {
NSDictionary *dict = [[placemarks objectAtIndex:0] addressDictionary];
NSLog(@"%@",[dict objectForKey:@"Country"]);
NSLog(@"%@",[dict objectForKey:@"State"]);
NSLog(@"%@",[dict objectForKey:@"SubLocality"]);
NSLog(@"%@",[dict objectForKey:@"Street"]);// Country(国家) State(城市) SubLocality(区) Street(详细)
} else {
NSLog(@"ERROR: %@", error); }
}];
[self.locMgr stopUpdatingLocation];
}