Cat Learn iOS corelocation anti-geocoding small demo input latitude and longitude get the city

Source: Internet
Author: User

Cat Share, must boutique

Original articles, welcome reprint. Reprint Please specify: Sanayu's Blog
Address: http://blog.csdn.net/u013357243

One: Effect

Input latitude and longitude, can get the corresponding place name

Second: Ideas

Almost identical to the field code.
1. Get the latitude and longitude of user input
2. Create a Cllocation object based on the latitude and longitude of the user input
3. Get the relevant landmark information based on the Cllocation object

Three: Code
#import "ViewController.h" #import <CoreLocation/CoreLocation.h>  @interface viewcontroller ()/** * GeoCode objects * /@property(nonatomic,Strong) Clgeocoder *geocoder;#pragma mark-anti-geocoding- (ibaction) Reversegeocode;@property(Weak,nonatomic)IboutletUitextfield *longtitudefield;@property(Weak,nonatomic)IboutletUitextfield *latitudefield;@property(Weak,nonatomic)Iboutlet UILabel*reversedetailaddresslabel;@end @implementation viewcontroller - (void) reversegeocode{//1. Get the latitude and longitude of user input    NSString*longtitude = Self. Longtitudefield. Text;NSString*latitude = Self. Latitudefield. Text;if(Longtitude. Length==0|| Longtitude = =Nil|| Latitude. Length==0|| Latitude = =Nil) {NSLog(@"Please enter latitude and longitude");return; }//2. Create a Cllocation object based on the latitude and longitude of user inputCllocation *location = [[Cllocation alloc] initwithlatitude:[latitude Doublevalue] Longitude:[longtitude DoubleValue] ;//3. Get the relevant landmark information based on the Cllocation object[ Self. GeocoderReversegeocodelocation:location completionhandler:^ (Nsarray*placemarks,Nserror*error) { for(Clplacemark *placemark in Placemarks) {NSLog(@"%@%@%f%f", Placemark. Name, Placemark. Addressdictionary, Placemark. location. Coordinate. Latitude, Placemark. location. Coordinate. Longitude); Self. Reversedetailaddresslabel. Text= Placemark. Locality; }    }];}#pragma mark-Lazy loading-(Clgeocoder *) geocoder{if(!_geocoder)    {_geocoder = [[Clgeocoder alloc] init]; }return_geocoder;}@end
Four: Knowledge expansion Clgeocoder

"Geocoding" and "anti-geocoding" can be completed using Clgeocoder
Geocoding: Based on a given place name. Get detailed location information (such as latitude and longitude, full name of address, etc.)
Anti-geocoding: Get detailed location information based on a given latitude and longitude

Geography Coding Method
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;
--Anti-geo-coding method
- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
Clgeocodecompletionhandler

When Geo-geocoding is complete, it calls the

typedefvoid (^CLGeocodeCompletionHandler)(NSArrayNSError *error);

This block transmits 2 parameters
Error: There is a value when the coding error (for example, the code does not have detailed information)
Placemarks: It contains Clplacemark objects.

Clplacemark

Clplacemark literally means a landmark, encapsulating detailed address location information

Location

@property (nonatomicreadonly) CLLocation *location;

Regional

@property (nonatomicreadonly) CLRegion *region;

Detailed address information

@property (nonatomicreadonlyNSDictionary *addressDictionary;

Address name

@property (nonatomicreadonlyNSString *name;

City

@property (nonatomicreadonlyNSString *locality;
Structure diagram

Cat Learn iOS corelocation anti-geocoding small demo input latitude and longitude get the city

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.