I. Clgeocoder geocoding and geo-coding
地理编码
:
- Obtain specific location information (such as latitude and longitude, the full name of the address, etc.) based on the given place name
// geo-coding methods -(void) Geocodeaddressstring: (nsstring*) Addressstringcompletionhandler: (Clgeocodecompletionhandler) Completionhandler;
- Anti-geocoding:
- Obtain specific location information based on a given latitude and longitude
Anti-geocoding Method-(void) Reversegeocodelocation: (cllocation*) Location Completionhandler: (Clgeocodecompletionhandler) Completionhandler;
+ Note:Clgeocodecompletionhandler-When geo-geocoding is complete, it ' calls Clgeocodecompletionhandler ' and can ' get to Clplacemark object ' "OBJC// This block pass 2 parameters //Error: When the coding error (such as coding not specific information) has the value //Placemarks: It contains a Clplacemark object typedef void (^clgeocodecompletionhandler) (nsarray*placemarks, nserror*error);
- Clplacemark (locality: City name thoroughfare: Street Name: Full name cllocation *location)
Second, the application scenario
- Used in conjunction with targeting to determine the specific address information for the current user
Third, examples
#import "ViewController.h"#import<CoreLocation/CoreLocation.h>@interfaceViewcontroller ()/** geo-coding*/@property (nonatomic, strong) Clgeocoder*GEOC, @property (weak, nonatomic) Iboutlet Uitextview*Addresstv, @property (weak, nonatomic) Iboutlet Uitextfield*LATITUDETF, @property (weak, nonatomic) Iboutlet Uitextfield*LONGITUDETF;@end@implementationViewcontroller#pragmaMark-Lazy Loading-(Clgeocoder *) geoc{if(!_GEOC) {_GEOC=[[Clgeocoder alloc] init]; } return_GEOC;}-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{ //[Self.view Endediting:yes];}/** geo-coding (address to Latitude)*/-(ibaction) geoCoder {nsstring*address =Self.addressTV.text; //Fault Tolerance if([address length] = =0) return; [SELF.GEOC geocodeaddressstring:address Completionhandler:^ (Nsarray<clplacemark *> * _nullable placemarks, Nserror *_nullable Error) { //Clplacemark: Landmarks//Location: Position Object//addressdictionary: Address dictionary//Name: Address Details//locality: City if(Error = =Nil) {Clplacemark*PL =[Placemarks Firstobject]; Self.addressTV.text=Pl.name; Self.latitudeTF.text=@ (pl.location.coordinate.latitude). StringValue; Self.longitudeTF.text=@ (pl.location.coordinate.longitude). StringValue; }Else{NSLog (@"Error"); } }];}-(ibaction) Reversegeocoder {//get the latitude and longitude of user input DoubleLatitude =[Self.latitudeTF.text Doublevalue]; DoubleLongitude =[Self.longitudeTF.text Doublevalue]; Cllocation*location =[[Cllocation alloc] Initwithlatitude:latitude longitude:longitude]; //anti-geocoding (latitude and longitude---address)[SELF.GEOC reversegeocodelocation:location completionhandler:^ (nsarray<clplacemark *> * _Nullable Placemarks, Nserror *_nullable Error) { if(Error = =Nil) {Clplacemark*PL =[Placemarks Firstobject]; Self.addressTV.text=Pl.name; Self.latitudeTF.text=@ (pl.location.coordinate.latitude). StringValue; Self.longitudeTF.text=@ (pl.location.coordinate.longitude). StringValue; }Else{NSLog (@"Error"); } }];}@end
IOS geocoding/Anti-geocoding