iOS Development extension Chapter-corelocation geocoding
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:
YYVIEWCONTROLLER.M file
1 //2 //YYVIEWCONTROLLER.M3 //19-Geocoding4 //5 //Created by Apple on 14-8-11.6 //Copyright (c) 2014 Yangyong. All rights reserved.7 //8 9 #import "YYViewController.h"Ten #import<CoreLocation/CoreLocation.h> One A @interfaceYyviewcontroller () -@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 Apple's map service provider in China is the gold map.