iOS Development extension Chapter-corelocation geocoding

Source: Internet
Author: User

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.M 3//19-geocoding 4//5//Created by Apple on 14-8-11. 6//Copyright (c) 2014 Yangyong.  All rights reserved.  7//8 9 #import "YYViewController.h" #import <CoreLocation/CoreLocation.h> @interface Yyviewcontroller () @property (Nonatomic,strong) Clgeocoder *geocoder; #pragma mark-Geographical Code-(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; #pragma mark-anti-geocoding-(ibaction) Reversegeocode; @property (Weak, nonatomic) Iboutlet Uitextfield *longitudefield; @property (Weak, nonatomic) Iboutlet Uitextfield *latitudefield; @property (Weak, nonatomic) Iboutlet UILabel *reverdedetailaddresslabel; @end @implementation yyviewcontroller #pragma mark-lazy load-(Clgeocoder *) Geocoder (_geocoder==nil) {_geocode _geocoder=[[clgeocoder alloc]init]; " R -(void) viewdidload x [[Super Viewdidload]; 42} 43/** 44 * geocoding: Place-name/latitude-Longitude coordinates */-(ibaction) GE      Ocode {47//1. To obtain the input address nsstring *address=self.addressfield.text; if (address.length==0) return; 50 51//2. Start Geocoding 52//Description: Call the method below to start encoding, regardless of whether the encoding succeeds or fails, calls the methods in block [Self.geocoder geocodeaddressstring:address Comp letionhandler:^ (Nsarray *placemarks, Nserror *error) {54//If there is an error message, or if the number of place-name elements obtained in the array is 0, then the description does not find the IF (Erro R | |         placemarks.count==0) {[email protected] "The address you entered was not found, possibly on the moon";}else//encoding succeeded, found the specific location information 58 {59//print to see all location information found. */* The name: Loca  Lity: City Country: National PostalCode: ZIP/Postal Code * * (Clplacemark *pLacemark in Placemarks) {NSLog (@ "name=%@ locality=%@ country=%@ postalcode=%@", Placemark.name,placemar K.locality,placemark.country,placemark.postalcode); 68} 69 70//Remove the first display in the obtained geographic information array on the interface Clplacemark *firstplacemark=[place Marks Firstobject]; 72//Detailed address name Self.detailaddresslabel.text=firstplacemark.name; 74//Latitude cllocationdegrees latitude=firstplacemark.location.coordinate.latitude; 76//Longitude cllocationdegrees longitude=firstplacemark.location.coordinate.longitude; Self.latitudelabel.text=[nsstring stringwithformat:@ "%.2f", Latitude]; Self.longitudelabel.text=[nsstring stringwithformat:@ "%.2f", longitude]; 80} 81}]; 82} 83 84/** 85 * Anti-geocoding: latitude/Longitude coordinates, place name (Ibaction) reversegeocode {88//1. Get input latitude and longitude NSString *long Titudetext=self.longitudefield.text; NSString *latitudEtext=self.latitudefield.text; if (longtitudetext.length==0| | latitudetext.length==0) return; Cllocationdegrees Latitude=[latitudetext Doublevalue]; 94 cllocationdegrees Longitude=[longtitudetext Doublevalue]; Cllocation *location=[[cllocation Alloc]initwithlatitude:latitude Longitude:longitude]; 97//2. Anti-geocoding 98 [Self.geocoder reversegeocodelocation:location completionhandler:^ (Nsarray *placemarks, NSError *e Rror) {if (error| |             placemarks.count==0) {[email protected] "The address you entered was not found, possibly on the moon"; 101}else//Encoded successfully 102 {103 Show the front landmark information 104 Clplacemark *firstplacemark=[placemarks firstobject];105 Self.reverdedeta iladdresslabel.text=firstplacemark.name;106//latitude and longitude 107 cllocationdegrees Latitude=firstplacemark.loc             ation.coordinate.latitude;108 cllocationdegrees longitude=firstplacemark.location.coordinate.longitude;109 Self.latitudeField.text=[nsstring stringwithformat:@ "%.2f", latitude];110 self.longitudefield.text=[nsstring StringWithForma t:@ "%.2f", longitude];111}112}];113}114-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event1 {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.