IOS geocoding and anti-geocoding

Source: Internet
Author: User

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

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.