iOS Development extension Chapter-corelocation geocoding

Source: Internet
Author: User

A simple explanation

Clgeocoder: Geo Encoder, where Geo is a shorthand for geographic English word geography.

1. Use Clgeocoder to complete the geocoding and anti-geocoding

  geocoding : Obtain specific location information (such as latitude and longitude, full name of address, etc.) based on a given place name

anti-geocoding : Obtain specific location information based on a given latitude and longitude

(1) Geocoding methods

-(void) geocodeaddressstring: (NSString *) addressstring Completionhandler: (Clgeocodecompletionhandler) Completionhandler;

(2) Anti-geo-coding method

-(void) Reversegeocodelocation: (cllocation *) location Completionhandler: (Clgeocodecompletionhandler) Completionhandler;

2.CLGeocodeCompletionHandler

When Geo-geocoding is complete, the Clgeocodecompletionhandler is called

  

This block passes 2 parameters

    Error : There is a value when the encoding is wrong (for example, no specific information is encoded)

    placemarks : It contains Clplacemark objects.

3.CLPlacemark

Description: Clplacemark literally means landmark, encapsulating detailed address location information

Geographical location @property (nonatomic, readonly) cllocation *location;

Regional @property (nonatomic, readonly) clregion *region;

Detailed address information @property (nonatomic, readonly) nsdictionary *addressdictionary;

Address name @property (nonatomic, readonly) NSString *name;

City @property (nonatomic, readonly) NSString *locality;

Second, the code example:

The interface in storyboard is as follows:

  

Implementation code:

VIEWCONTROLLER.M file

1 //2 //YYVIEWCONTROLLER.M3 //geo-coding4 //5 //Created by Xiaozeng on 14-8-11.6 //Copyright (c) 2014 GitHub. All rights reserved.7 //8 9 #import "ViewController.h"Ten #import<CoreLocation/CoreLocation.h> One  A @interfaceViewcontroller () -@property (nonatomic,strong) Clgeocoder *Geocoder; - #pragmamark-geocoding the-(ibaction) GeoCode; -@property (Weak, nonatomic) Iboutlet Uitextfield *Addressfield; -@property (Weak, nonatomic) Iboutlet UILabel *Longitudelabel; -@property (Weak, nonatomic) Iboutlet UILabel *Latitudelabel; +@property (Weak, nonatomic) Iboutlet UILabel *Detailaddresslabel; -  + #pragmamark-Anti-geo-coding A  at-(ibaction) Reversegeocode; -@property (Weak, nonatomic) Iboutlet Uitextfield *Longitudefield; -@property (Weak, nonatomic) Iboutlet Uitextfield *Latitudefield; -@property (Weak, nonatomic) Iboutlet UILabel *Reverdedetailaddresslabel; - @end -  in @implementationYyviewcontroller -  to #pragmamark-Lazy Loading +-(Clgeocoder *) Geocoder - { the     if(_geocoder = =Nil) { *_geocoder =[[Clgeocoder alloc] init]; $     }Panax Notoginseng     return_geocoder; - } the- (void) Viewdidload + { A [Super Viewdidload]; the } + /** - * geo-coding: Geographical Names--latitude and longitude coordinates $  */ $-(ibaction) GeoCode { -     // 1. Get the address you entered -NSString *address =Self.addressField.text; the     if(Address.length = =0)return; -     Wuyi     // 2. Start geocoding the     // Description: Call the following method to start coding, regardless of whether the encoding succeeds or fails, calls the method in block -[Self.geocoder geocodeaddressstring:address completionhandler:^ (Nsarray *placemarks, NSError *error) { Wu         // If there is an error message, or if the number of place-name elements obtained in the array is 0, then the description is not found -         if(Error | | placemarks.count = =0) { Aboutself.detailaddresslabel.text=@"the address you entered was not found, possibly on the moon."; $}Else   //The coding was successful and the location information was found. -         { -             // Print to see all the location information found -                 /* A Name: Names + locality: City the Country: Country - postalCode: Zip code $                  */ the              for(Clplacemark *placemarkinchplacemarks) { theNSLog (@"name=%@ locality=%@ country=%@ postalcode=%@", Placemark.name, placemark.locality, Placemark.country, placemark.postalcode); the             } the              -             // Remove the first of the obtained geo-information array displayed on the interface inClplacemark *firstplacemark =[Placemarks firstobject]; the             // Detailed address name theSelf.detailAddressLabel.text =Firstplacemark.name; About             // Latitude theCllocationdegrees latitude =FirstPlacemark.location.coordinate.latitude; the             // Longitude theCllocationdegrees longitude =FirstPlacemark.location.coordinate.longitude; +Self.latitudeLabel.text = [NSString stringWithFormat:@"%.2f", latitude]; -Self.longitudeLabel.text = [NSString stringWithFormat:@"%.2f", longitude]; the         }Bayi     }]; the } the  - /** - * Anti-geocoding: Geographical Names by latitude and longitude coordinates the  */ the-(ibaction) Reversegeocode { the     // 1. Get the latitude and longitude of the input theNSString *longtitudetext =Self.longitudeField.text; -NSString *latitudetext =Self.latitudeField.text; the     if(Longtitudetext.length = =0|| Latitudetext.length = =0)return; the      theCllocationdegrees latitude =[Latitudetext Doublevalue];94Cllocationdegrees longitude =[Longtitudetext Doublevalue]; the      theCllocation *location =[[Cllocation alloc] Initwithlatitude:latitude longitude:longitude]; the     // 2. Anti-geo-coding98[Self.geocoder reversegeocodelocation:location completionhandler:^ (Nsarray *placemarks, NSError *error) { About         if(Error | | placemarks.count = =0) { -Self.reverdeDetailAddressLabel.text =@"the address you entered was not found, possibly on the moon.";101}Else//Coding Success102         {103             //Show the front landmark information104Clplacemark *firstplacemark =[Placemarks firstobject]; theSelf.reverdeDetailAddressLabel.text =Firstplacemark.name;106             //latitude and longitude degree107Cllocationdegrees latitude =FirstPlacemark.location.coordinate.latitude;108Cllocationdegrees longitude =FirstPlacemark.location.coordinate.longitude;109Self.latitudeField.text = [NSString stringWithFormat:@"%.2f", latitude]; theSelf.longitudeField.text = [NSString stringWithFormat:@"%.2f", longitude];111         } the     }];113 } the  the-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event the {117 [Self.view Endediting:yes];118 }119 @end

Implementation results:

(1) Geocoding: (geographical name, latitude and longitude coordinates)

  

Print output:

  

(2) Anti-geocoding: (Geographical names, longitude and latitude)

   

(3) Note: Adjust the keyboard

  

Click the latitude and longitude TextField for input, the keyboard appears as follows

  

(4) Note: All the results of the search are in China, because the Apple Map service provider in China is the gold map.

iOS Development extension Chapter-corelocation 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.