Geographic Encoding and anti-Geographic Encoding of iOS native maps, and Geographic Encoding of ios maps

Source: Internet
Author: User

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

*/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.