iOS 根据经纬度获取地名,根据地名获取经纬度

1. 根据经纬度获取地名

[objc]  view plain  copy
 
  1. CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init];  
  2. [clGeoCoder reverseGeocodeLocation:newLocation completionHandler: ^(NSArray *placemarks,NSError *error) {  
  3.      for (CLPlacemark *placeMark in placemarks)  
  4.      {  
  5.          NSDictionary *addressDic=placeMark.addressDictionary;  
  6.            
  7.          NSString *state=[addressDic objectForKey:@"State"];  
  8.          NSString *city=[addressDic objectForKey:@"City"];  
  9.          NSString *subLocality=[addressDic objectForKey:@"SubLocality"];  
  10.          NSString *street=[addressDic objectForKey:@"Street"];  
  11.            
  12.          [self stopLocation];  
  13.          [_chooseCityBtn setTitle:city forState:UIControlStateNormal];  
  14.          [_activityIndicator stopAnimating];  
  15.      }  
  16.        
  17.  }];  

2. 根据地名获取经纬度

[objc]  view plain  copy
 
  1. NSString *oreillyAddress = @"1005 Gravenstein Highway North, Sebastopol, CA 95472, USA";  
  2. CLGeocoder *myGeocoder = [[CLGeocoder alloc] init];  
  3. [myGeocoder geocodeAddressString:oreillyAddress completionHandler:^(NSArray *placemarks, NSError *error) {  
  4.     if ([placemarks count] > 0 && error == nil) {  
  5.         NSLog(@"Found %lu placemark(s).", (unsigned long)[placemarks count]);  
  6.         CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];  
  7.         NSLog(@"Longitude = %f", firstPlacemark.location.coordinate.longitude);  
  8.         NSLog(@"Latitude = %f", firstPlacemark.location.coordinate.latitude);  
  9.     }  
  10.     else if ([placemarks count] == 0 && error == nil) {  
  11.         NSLog(@"Found no placemarks.");  
  12.     } else if (error != nil) {  
  13.         NSLog(@"An error occurred = %@", error);  
  14.     }  
  15. }];  

你可能感兴趣的:(iOS)