When we want to implement the function in the app: Enter place name, code for latitude and longitude, realize navigation function. So, I need to use the Geocoding feature in the native map, and in the core location, it includes the positioning, geocoding (including the anti-coding) feature.
Import in File
#import <CoreLocation/CoreLocation.h>
Geocoding:
/** geocoding */-(void) Geocoder { clgeocoder *geocoder=[[clgeocoder alloc]init]; NSString *addressstr = @ "Guangdong province Shenzhen Bao ' an district";//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); }]; }
Geo-coding:
/** geo-*/-(void) reversegeocoder{ //Create GeoCode Objects 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-geocoding [Geocoder reversegeocodelocation:location completionhandler:^ (nsarray<clplacemark *> * _Nullable Placemarks, Nserror * _nullable error) { //To determine if there is an error or if Placemarks is empty if (Error!=nil | | placemarks.count==0) { C12/>nslog (@ "%@", error); return; } For (Clplacemark *placemark in placemarks) { //detail address nsstring *addressstr = placemark.name; NSLog (@ "Detailed address 1:%@", addressstr); NSLog (@ "Detailed address 2:%@", placemark.addressdictionary); NSLog (@ "Detailed address 3:%@", placemark.locality); } ];}
Demo:https://github.com/jnkindle/geocoderandreversereocoder
/**
* AUTHOR:JN
* Github:https://github.com/jnkindle
* Cnblogs:http://www.cnblogs.com/jnkindle
* qq:1294405741
*/
IOS native Map geocoding and anti-geocoding