Detailed Explanation of iOS Native Map Geocoding and Anti Geocoding of
- 2021-12-21 05:16:45
- OfStack
When we want to realize the function in App: input the land name, encode it as longitude and latitude, and realize the navigation function.
Then, I need to use the geocoding function in the native map, while Core Location mainly includes positioning and geocoding (including anti-coding) functions.
Import in File
#import <CoreLocation/CoreLocation.h>
Geocoding:
/**
Geocoding
*/
- (void)geocoder {
CLGeocoder *geocoder=[[CLGeocoder alloc]init];
NSString *addressStr = @" Baoan District, Shenzhen City, Guangdong Province ";// Location information
[geocoder geocodeAddressString:addressStr completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (error!=nil || placemarks.count==0) {
return ;
}
// Create placemark Object
CLPlacemark *placemark=[placemarks firstObject];
// Longitude
NSString *longitude =[NSString stringWithFormat:@"%f",placemark.location.coordinate.longitude];
// Latitude
NSString *latitude =[NSString stringWithFormat:@"%f",placemark.location.coordinate.latitude];
NSLog(@" Longitude: %@ Latitude: %@",longitude,latitude);
}];
}
Geographic back coding:
/**
Geographic back coding
*/
- (void)reverseGeocoder{
// Create a geocoding object
CLGeocoder *geocoder=[[CLGeocoder alloc]init];
// Longitude
NSString *longitude = @"113.23";
// Latitude
NSString *latitude = @"23.16";
// Creation location
CLLocation *location=[[CLLocation alloc]initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
// Inverse geocoding
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
// Judge whether there is an error or placemarks Whether it is empty or not
if (error !=nil || placemarks.count==0) {
NSLog(@"%@",error);
return ;
}
for (CLPlacemark *placemark in placemarks) {
// Detailed address
NSString *addressStr = placemark.name;
NSLog(@" Detailed address 1 : %@",addressStr);
NSLog(@" Detailed address 2 : %@",placemark.addressDictionary);
NSLog(@" Detailed address 3 : %@",placemark.locality);
}
}];
}