Map article-02. geocoding

Source: Internet
Author: User

Map-02. geocoding (geocoding/reversegeocoding)

The previous section gives you a brief introduction to getting the user's location, using latitude and longitude to represent the location. Let's talk about geocoding in this section.

First, we need to know what geocoding is

Concept:

Geocoding: Refers to the process of establishing a spatial coordinate relationship between statistical data or address information, called geocoding. Implements the ability to convert a Chinese address or a place-name description to a latitude representation on a map (on the earth's surface).

Anti-geocoding: Enables the conversion of latitude and longitude on the map (on the earth's surface) to a Chinese address or a place-name description.

Prepare before encoding:

Before writing the code, drag several controls in the storyboard, such as:

After you drag these controls, be sure to initialize the Tabbarcontroller,

After completing the above steps, we're going to start writing code.

Because you want to display two uiviewcontroller on a view, two classes are created and associated

Once you're connected, drag the controls we need into the appropriate class.

Now it's time to start writing code (FOCUS):

First, import the header file

#import <corelocation/corelocation.h>, in the previous section, said that if there are many classes to use, you can write a PCH file, here is a simple way to say:

When you finish the previous step, you'll find that Corelocation is still not available in the class, don't worry, and the next step:

On the code:

Geo-coding (GeoCoding):

1 //2 //GEOCODVIEWCONTROLLER.M3 //geo-coding4 //5 //Created by admin on 16/5/21.6 //copyright©2016 year KXZDJ. All rights reserved.7 //8 9 #import "GeocodViewController.h"Ten  One @interfaceGeocodviewcontroller () A@property (Weak, nonatomic) Iboutlet Uitextfield *addressfiled; -@property (Weak, nonatomic) Iboutlet UILabel *Latitudelabel; -@property (Weak, nonatomic) Iboutlet UILabel *Lontitudelabel; the@property (Weak, nonatomic) iboutlet Uitextview *Detailtextview; -  - @end -  + @implementationGeocodviewcontroller --(Ibaction) GeoCode: (ID) Sender { +      A     //Create a Geocoding object atClgeocoder *geo =[[Clgeocoder alloc] init]; -      -     //geo-coding -[Geo GeocodeAddressString:self.addressFiled.text completionhandler:^ (Nsarray<clplacemark *> * _nullable Placemarks, Nserror *_nullable Error) { -         //determine Placemarks If no value or error exists, print error message -         if(Placemarks.count = =0||error) { inNSLog (@"%@", error); -             return; to         } +          -         //Get a landmark theClplacemark *cpl =[Placemarks lastobject]; *          $         //LatitudePanax NotoginsengCllocationdegrees latitude =Cpl.location.coordinate.latitude; -          the         //Longitude +Cllocationdegrees longtitude =Cpl.location.coordinate.longitude; A          the         //Assignment (note that latitude and longitude are double types) +Self.latitudeLabel.text = [NSString stringWithFormat:@"%f", latitude]; -Self.lontitudeLabel.text = [NSString stringWithFormat:@"%f", longtitude]; $          $         //defines a temporary string to receive the name of the address obtained -NSString *tempstr =@""; -          the         //Traverse Placemarks to get the current coordinate position -          for(Clplacemark *cplinchplacemarks) {Wuyi             //Why do we append strings here? Because Placemarks is an array, there are a lot of names in the same place (for example: Shanghai has a Nanjing road pedestrian street, Chengdu has a pedestrian street, when the user input pedestrian street, these two will show up, if not added, will only show one)  theTempStr = [TempStr stringbyappendingstring:[nsstring stringWithFormat:@"%@\n", Cpl.name]]; -         } Wu          -         //assign a value to Self.detailtextview AboutSelf.detailTextView.text =TempStr; $          -          -     }]; - } A  +- (void) Viewdidload { the [Super Viewdidload]; -      $ } the  the  the @end

Run:

Now you should understand the meaning of placemarks and the concatenation of strings in your code.

Anti-geocoding (reversegeocoding):

1 //2 //REVERSECODEVIEWCONTROLLER.M3 //geo-coding4 //5 //Created by admin on 16/5/21.6 //copyright©2016 year KXZDJ. All rights reserved.7 //8 9 #import "ReverseCodeViewController.h"Ten  One  A @interfaceReversecodeviewcontroller () -@property (Weak, nonatomic) Iboutlet Uitextfield *Latitudelabel; -@property (Weak, nonatomic) Iboutlet Uitextfield *Longtitudelabel; the@property (Weak, nonatomic) iboutlet Uitextview *Detailtextview; -  - @end -  + @implementationReversecodeviewcontroller -  +- (void) Viewdidload { A [Super Viewdidload]; at      - } --(Ibaction) Reversecode: (ID) Sender { -     //Create geocoding -Clgeocoder *reversecode =[[Clgeocoder alloc] init]; -      in     //Latitude -Cllocationdegrees latitude =[Self.latitudeLabel.text Doublevalue]; to      +     //Longitude -Cllocationdegrees longitude =[Self.longtitudeLabel.text Doublevalue]; the      *     //Create a geographic location $Cllocation *local =[[Cllocation alloc] Initwithlatitude:latitude longitude:longitude];Panax Notoginseng      -     //Anti-geo-coding the[Reversecode reversegeocodelocation:local completionhandler:^ (nsarray<clplacemark *> * _Nullable Placemarks, Nserror *_nullable Error) { +         //determine Placemarks If no value or error exists, print error message A         if(Placemarks.count = =0||error) { theNSLog (@"Geocoding failed"); +             return; -         } $          $NSLog (@"%ld", placemarks.count); -          -         //Get Address theClplacemark *cpl =[Placemarks lastobject]; -NSLog (@"%@", cpl.name);Wuyi          the         //assign a value to Detailtextview -Self.detailTextView.text =Cpl.name; Wu     }]; -      About      $ } -  - @end

Run:

In the anti-geocoding time to pay attention to:

1. If the latitude and longitude of the input is present on the map, but the encoding fails, do not worry, it is because the system will automatically determine which country you are in, and then you are only allowed to access the latitude and longitude in this country.

2. Anti-geocoding you may find that there is only one value in the array, which confirms the uniqueness of a specific latitude and longitude.

Geocoding and anti-geocoding here is the end, I hope to help everyone, if there is no place or the wrong words please tell me, I will be the first time to correct. Thank you.

Map article-02. 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.