iOS开发中最基本的位置功能实现示例

2020-01-14 16:25:30王冬梅

NSLog(@"kABPersonAddressCityKey = %@", (NSString *)kABPersonAddressCityKey); 
NSLog(@"city = %@", place.addressDictionary[(NSString *)kABPersonAddressCityKey]); 
NSString *city = place.locality; 
if(city == nil) 

city = place.addressDictionary[(NSString *)kABPersonAddressStateKey]; 


}]; 



- (void)didReceiveMemoryWarning 

[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 


@end
程序运行结果:(以39.3,116.4为例)

 

iOS开发中最基本的位置功能实现示例

 

复制代码
//  判断输入的地址  
if (self.locationTextField.text == nil  ||  [self.locationTextField.text length] == 0) {  
    return;  
}  
  
CLGeocoder *geocoder = [[CLGeocoder alloc] init];  
/*  -----------------------------------------位置编码--------------------------------------------  */  
[geocoder geocodeAddressString:_locationTextField.text completionHandler:^(NSArray *placemarks, NSError *error) {  
      
    for (CLPlacemark *placemark in placemarks) {  
          
        CLLocationCoordinate2D coordinate = placemark.location.coordinate;  
        NSString *strCoordinate = [NSString stringWithFormat:@"纬度 = %3.5fn 经度 = %3.5f",coordinate.latitude,coordinate.longitude];  
        NSLog(@"%@",strCoordinate);  
        NSDictionary *addressDictionary = placemark.addressDictionary;  
        NSString *address = [addressDictionary objectForKey:(NSString *)kABPersonAddressStreetKey];  
        NSString *state = [addressDictionary objectForKey:(NSString *)kABPersonAddressStateKey];  
        NSString *city = [addressDictionary objectForKey:(NSString *)kABPersonAddressCityKey];