(77) Geo-coding and anti-geocoding

Source: Internet
Author: User

The so-called geocoding refers to acquiring location information through place names, such as latitude and longitude, detailed addresses, and so on.

The so-called anti-geo-coding, refers to the latitude and longitude, altitude and other information to obtain geographical location information.

The use of geocoding and anti-geocoding on iOS, if the manual input latitude and longitude, is not required to obtain user authorization, but generally is to obtain the user's latitude and longitude, and then through the geographical coding to achieve precise positioning, and therefore need authorization, this article because is a separate explanation of the relevant knowledge of geocoding, so the use of manual transmission of latitude and longitude Do not repeat the authorization code.

① Import Frame:

#import <CoreLocation/CoreLocation.h>

② New Clgeocoder object:

@property (Strong, nonatomic) Clgeocoder *gcoder;
Clgeocoder *coder = [[Clgeocoder alloc] Init];self.gcoder = coder;

"Geocoding"

③ invokes the Geocodeaddressstring:completionhandler method of the object, passing in a string place name and returning the object as a landmark array.

In general, there is only one element in the landmark array, and a landmark is obtained by Firstobject or Lastobject.

Each landmark has a number of properties, printing a landmark, you can find that the properties are too complex, where the more useful attributes or through the landmark class Clplacemark header files, the more important list is as follows:

Location: Position information can be obtained latitude and longitude, altitude, etc., the above section is introduced.

City: Name

Name: Full place name

Addressdictionary: This is a dictionary, the data is as follows

{City    = "\u5317\u4eac\u5e02\u5e02\u8f96\u533a";    Country = "\U4E2D\U56FD";    CountryCode = CN;    Formattedaddresslines =     (        "\U4E2D\U56FD\U5317\U4EAC\U5E02"    );    Name = "\u5317\u4eac\u5e02";    State = "\U5317\U4EAC\U5E02";}
Where the formatteraddresslines is the formatted address, the parentheses represent the array in which the element is a string.

The following code obtains and outputs the latitude and longitude and the formatted address, and those member properties that start with self are textfield for display.

    NSString *addressstr = Self.addressView.text;    if (addressstr = = Nil | | addressstr.length = = 0) {NSLog (@ "Please enter Address"); } [Self.gcoder geocodeaddressstring:addressstr completionhandler:^ (Nsarray *placemarks, NSError *error) {//landmark array        Stores a landmark, each landmark, containing a series of information about the current location.        Clplacemark *placemark = [Placemarks firstobject]; NSLog (@ "%@%@%f%f", Placemark.name, Placemark.addressdictionary,placemark.location.coordinate.latitude,        Placemark.location.coordinate.longitude);        Self.latiView.text = [NSString stringwithformat:@ "%f", Placemark.location.coordinate.latitude];                Self.longtiView.text = [NSString stringwithformat:@ "%f", Placemark.location.coordinate.longitude];        The Formattedaddresslines in Addressdictionary is an array that is stored internally as a string nsmutablestring *STRM = [nsmutablestring string];        Nsarray *addresslines = placemark.addressdictionary[@ "Formattedaddresslines"];       For (NSString *str in Addresslines) {[StrM appendstring:str]; } self.detailView.text = StrM;        if (Error) {NSLog (@ "%@", error); }            }];

"Anti-geocoding"

④ call reversegeocodelocation method, pass in a cllocation can, get is still a landmark, and the previous processing, it should be noted that cllocation if you want to enter a value, to be set at initialization, because latitude and longitude, altitude, etc. are read-only properties , so it should be assigned at initialization time, the following code obtains the latitude and longitude by acquiring TextField, and then the inverse geocoding obtains the location information:

    Cllocationdegrees latitude = [Self.latiView.text doublevalue];        Cllocationdegrees longtitude = [Self.longtiView.text doublevalue];    Cllocation *location = [[Cllocation alloc] Initwithlatitude:latitude longitude:longtitude]; [Self.gcoder reversegeocodelocation:location completionhandler:^ (Nsarray *placemarks, NSError *error) {for (Clplacemark *placemark in Placemarks) {NSLog (@ "%@%@%f%f", Placemark.name, Placemar                        K.addressdictionary,placemark.location.coordinate.latitude,placemark.location.coordinate.longitude);                        Self.addressView.text = placemark.addressdictionary[@ "City"];            nsmutablestring *STRM = [nsmutablestring string];            Nsarray *addresslines = placemark.addressdictionary[@ "Formattedaddresslines"];            For (NSString *str in Addresslines) {[StrM appendstring:str];                                } self.detailView.text = StrM; } if (erROR) {NSLog (@ "%@", error); }    }];

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

(77) Geo-coding 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.