SoFunction
Updated on 2025-04-13

iOS native map geocoding and anti-geocoding (detailed explanation)

When we want to implement functions in the App: enter the place name, encode it as latitude and longitude, and realize the navigation function.

So, I need to use the geocoding function in the native map, and Core Location mainly includes location and geocoding (including decoding) functions.

Import in a file

#import <CoreLocation/>

Geocode:

/**
  Geocode
  */
- (void)geocoder { 
   
  CLGeocoder *geocoder=[[CLGeocoder alloc]init]; 
   
  NSString *addressStr = @"Bao'an District, Shenzhen, Guangdong Province";//Location information   
  [geocoder geocodeAddressString:addressStr completionHandler:^(NSArray&lt;CLPlacemark *&gt; * _Nullable placemarks, NSError * _Nullable error) { 
    if (error!=nil || ==0) { 
      return ; 
    } 
    //Create placemark object    CLPlacemark *placemark=[placemarks firstObject]; 
    //longitude    NSString *longitude =[NSString stringWithFormat:@"%f",]; 
    //latitude    NSString *latitude =[NSString stringWithFormat:@"%f",]; 
     
    NSLog(@"Longitude: %@, latitude: %@",longitude,latitude); 
     
  }]; 
   
}

Geographic decoding:

/**
  Geographic decoding
  */
- (void)reverseGeocoder{ 
  //Create a geocoding object  CLGeocoder *geocoder=[[CLGeocoder alloc]init]; 
   
  //longitude  NSString *longitude = @"113.23"; 
  //latitude  NSString *latitude = @"23.16"; 
   
   
  //Create location  CLLocation *location=[[CLLocation alloc]initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]]; 
   
   
  //Anti-geography encoding  [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray&lt;CLPlacemark *&gt; * _Nullable placemarks, NSError * _Nullable error) { 
    //Judge whether there is any error or whether placemarks is empty    if (error !=nil || ==0) { 
      NSLog(@"%@",error); 
      return ; 
    } 
    for (CLPlacemark *placemark in placemarks) { 
      //Detailed address      NSString *addressStr = ; 
      NSLog(@"Detailed address 1: %@",addressStr); 
      NSLog(@"Detailed address 2: %@",); 
      NSLog(@"Detailed address 3: %@",); 
    } 
     
  }]; 
}

The above article on iOS native map geocoding and anti-geocode (detailed explanation) is all the content I share with you. I hope you can give you a reference and I hope you can support me more.