1.UI build, Import header file
#import <CoreLocation/CoreLocation.h>
2. Add a member variable and connect
1-(ibaction) Geocodebutton;2@property (Weak, nonatomic) Iboutlet Uitextfield *inputaddress;3@property (Weak, nonatomic) Iboutlet Uitextfield *longitude;4@property (Weak, nonatomic) Iboutlet Uitextfield *latitude;5@property (Weak, nonatomic) iboutlet Uitextview *detailaddress;6 7 8-(ibaction) Ungeocodebutton;9@property (Weak, nonatomic) Iboutlet Uitextfield *Reverselongitude;Ten@property (Weak, nonatomic) Iboutlet Uitextfield *Reverselatitude; One@property (Weak, nonatomic) Iboutlet Uitextview *reversedetailaddress;
3. Adding geocoding and anti-geocoding methods
1 /**2 * Lazy Loading3 */4-(Clgeocoder *) Geocoder5 {6 if(_geocoder = =Nil) {7_geocoder =[[Clgeocoder alloc]init];8 }9 return_geocoder;Ten } One A /** - * Keyboard Handling - */ the- (void) touchesended: (Nsset *) touches withevent: (Uievent *)Event - { - [Self.view Endediting:yes]; - } + - /** + * geo-coding A */ at-(ibaction) Geocodebutton { -NSString *address =Self.inputAddress.text; -[Self.geocoder geocodeaddressstring:address completionhandler:^ (Nsarray *placemarks, NSError *error) { - //address is empty, return directly - if(!address)return ; - if(Error) {//the address you entered has an error inSelf.detailAddress.text =@"the address you entered may not exist"; -}Else{ to //traverse a query to a landmark +NSLog (@"%d landmarks in total meet your requirements", placemarks.count); - for(inti =0; i < Placemarks.count; i++) { theClplacemark *placemark =Placemarks[i]; *NSLog (@"%@", placemark); $ }Panax Notoginseng - //take the first of the landmark array as the final result theClplacemark *placemark =[Placemarks firstobject]; +Self.detailAddress.text =Placemark.name; ASelf.latitude.text =[nsstring stringWithFormat:@"%.1f", Placemark.location.coordinate.latitude]; theSelf.longitude.text = [NSString stringWithFormat:@"%.1f", Placemark.location.coordinate.longitude]; + } - }]; $ } $ - /** - * Anti-geo-coding the */ --(ibaction) Ungeocodebutton {Wuyi //latitude and longitude conversion theCllocationdegrees longitude =[Self.reverseLongitude.text Doublevalue]; -Cllocationdegrees latitude =[Self.reverseLatitude.text Doublevalue]; WuCllocation *location =[[Cllocation alloc]initwithlatitude:latitude longitude:longitude]; - About //Anti-geo-coding $[Self.geocoder reversegeocodelocation:location completionhandler:^ (Nsarray *placemarks, NSError *error) { - if(location = = nil)return; - if(Error) { -NSLog (@"you entered the wrong latitude and longitude."); A}Else{ + //traversing a landmark array theNSLog (@"%d landmarks in total meet your requirements", placemarks.count); - for(intI i < Placemarks.count; i++) { $Clplacemark *placemark =Placemarks[i]; theNSLog (@"%@", placemark); the } the the //take the first of the landmark array as the final result -Clplacemark *placemark =[Placemarks firstobject]; inSelf.reverseDetailAddress.text =Placemark.name; the } the About }]; the}
IOS geocoding and anti-geocoding