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