Geocoding, as the name implies, is applied to the positioning function, following a brief description of the geocoding in Cllocation:
Interface:
Code:
1 #import "HMViewController.h"2 #import<CoreLocation/CoreLocation.h>3 4 @interfaceHmviewcontroller ()5@property (nonatomic, strong) Clgeocoder *Geocoder;6 7 #pragmaMark-geo-coding8-(ibaction) GeoCode;9@property (Weak, nonatomic) Iboutlet Uitextfield *Addressfield;Ten@property (Weak, nonatomic) Iboutlet UILabel *Longitudelabel; One@property (Weak, nonatomic) Iboutlet UILabel *Latitudelabel; A@property (Weak, nonatomic) Iboutlet UILabel *Detailaddresslabel; - - #pragmaMark-Anti-geocoding the-(ibaction) Reversegeocode; -@property (Weak, nonatomic) Iboutlet Uitextfield *Longtitudefield; -@property (Weak, nonatomic) Iboutlet Uitextfield *Latitudefield; -@property (Weak, nonatomic) Iboutlet UILabel *Reversedetailaddresslabel; + @end - + @implementationHmviewcontroller A at-(Clgeocoder *) Geocoder - { - if(!_geocoder) { -Self.geocoder =[[Clgeocoder alloc] init]; - } - return_geocoder; in } - to- (void) Viewdidload + { - [Super Viewdidload]; the * } $ Panax Notoginseng /** - * Geographical code: Geographical name, longitude and latitude the */ +- (void) GeoCode A { the //1. Get the address you entered +NSString *address =Self.addressField.text; - if(Address.length = =0)return; $ $ //2. Start Coding -[Self.geocoder geocodeaddressstring:address completionhandler:^ (Nsarray *placemarks, NSError *error) { - if(Error | | placemarks.count = =0) { theSelf.detailAddressLabel.text =@"the address you entered could not be found, possibly on Mars ."; -}Else{//Successful encoding (location information is found)Wuyi //all the landmark information of the output query the for(Clplacemark *placemarkinchplacemarks) { -NSLog (@"name=%@ locality=%@ country=%@ postalcode=%@", Placemark.name, placemark.locality, Placemark.country, placemark.postalcode); Wu } - About //Show the front landmark information $Clplacemark *firstplacemark =[Placemarks firstobject]; -Self.detailAddressLabel.text =Firstplacemark.name; - -Cllocationdegrees latitude =FirstPlacemark.location.coordinate.latitude; ACllocationdegrees longitude =FirstPlacemark.location.coordinate.longitude; +Self.latitudeLabel.text = [NSString stringWithFormat:@"%.2f", latitude]; theSelf.longitudeLabel.text = [NSString stringWithFormat:@"%.2f", longitude]; - } $ }]; the } the the /** the * Anti-geocoding: Geographical Names by latitude and longitude - */ in- (void) Reversegeocode the { theNSString *longtitudetext =Self.longtitudeField.text; AboutNSString *latitudetext =Self.latitudeField.text; the if(Longtitudetext.length = =0|| Latitudetext.length = =0)return; the theCllocationdegrees latitude =[Latitudetext Doublevalue]; +Cllocationdegrees longtitude =[Longtitudetext Doublevalue]; - the //Start Reverse CodingBayiCllocation *location =[[Cllocation alloc] Initwithlatitude:latitude longitude:longtitude]; the[Self.geocoder reversegeocodelocation:location completionhandler:^ (Nsarray *placemarks, NSError *error) { the if(Error | | placemarks.count = =0) { -Self.reverseDetailAddressLabel.text =@"the latitude and longitude you entered could not be found, possibly on Mars ."; -}Else{//Successful encoding (location information is found) the //all the landmark information of the output query the for(Clplacemark *placemarkinchplacemarks) { theNSLog (@"name=%@ locality=%@ country=%@ postalcode=%@", Placemark.name, placemark.locality, Placemark.country, placemark.postalcode); the } - the //Show the front landmark information theClplacemark *firstplacemark =[Placemarks firstobject]; theSelf.reverseDetailAddressLabel.text =Firstplacemark.name;94 theCllocationdegrees latitude =FirstPlacemark.location.coordinate.latitude; theCllocationdegrees longitude =FirstPlacemark.location.coordinate.longitude; theSelf.latitudeField.text = [NSString stringWithFormat:@"%.2f", latitude];98Self.longtitudeField.text = [NSString stringWithFormat:@"%.2f", longitude]; About } - }];101 }102 103- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event104 { the [Self.view Endediting:yes];106 }107 108 @end
Initial use of cllocation in Xcode-geocoding