Geographic Encoding and anti-Geographic Encoding of iOS native maps, and Geographic Encoding of ios maps
When we want to implement the function in the App: Enter the place name, encode it as the latitude and longitude, and implement the navigation function. Therefore, I need to use the Geographic Encoding function in the native map, and the Core Location mainly includes the Location and Geographic Encoding (including anti-encoding) functions.
Import in file
# Import <CoreLocation/CoreLocation. h>
Geocode:
/** Geocode */-(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 the placemark object CLPlacemark * placemark = [placemarks firstObject]; // the longitude NSString * longpolling = [NSString stringWithFormat: @ "% f ", placemark. location. coordinate. longpolling]; // latitude NSString * latitude = [NSString stringWithFormat: @ "% f", placemark. location. coordinate. latitude]; NSLog (@ "longitude: % @, latitude: % @", longpolling, latitude) ;}];}
Anti-encoding:
/** Geocoder */-(void) reverseGeocoder {// create a geocode object CLGeocoder * geocoder = [[CLGeocoder alloc] init]; // longitude NSString * longpolling = @ "113.23"; // latitude NSString * latitude = @ "23.16"; // create a location CLLocation * location = [[CLLocation alloc] initWithLatitude: [latitude floatValue] longpolling: [longpolling floatValue]; // anti-geocode [geocoder reverseGeocodeLocation: location completionHandler: ^ (NSArray <CLPlacemark *> * _ Nullable placem Arks, NSError * _ Nullable error) {// determines whether an error exists or whether placemarks is null 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) ;}}] ;}
Demo: https://github.com/JnKindle/GeocoderAndReverseReocoder
/**
* Author: Jn
* GitHub: https://github.com/JnKindle
* Cnblogs: http://www.cnblogs.com/JnKindle
* QQ: 1294405741
*/