iOS development: Get City, province and other information through latitude and longitude

Source: Internet
Author: User

iOS system with the positioning, with Cllocationmanager can easily achieve the positioning of the operation, obtained is a set of latitude and longitude, of course, you can also obtain the corresponding provinces, cities, streets and other information according to the latitude and longitude, the following look at a city according to latitude and longitude of the demo:


Because the Cllocationmanager class is required to obtain the latitude and longitude, and this class is contained in the Corelocation framework, obtaining the city information requires a Mapkit framework, so you need to first import the two frameworks into the project:

To import a frame: Select 1.target--2.build phases--3.link Binary with libraries--4. Click on the "+" sign: Step:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/42/75/wKioL1PXqp7DYcryAAOBjKWmsyk065.jpg "title=" Qq20140729-1.png "alt=" Wkiol1pxqp7dycryaaobjkwmsyk065.jpg "/>


After clicking the plus sign, enter the appropriate frame in the search box to search:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/42/75/wKiom1PXqh6StAPgAACn05SwfcA815.jpg "style=" float: none; "title=" Qq20140729-2.png "alt=" Wkiom1pxqh6stapgaacn05swfca815.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/42/75/wKioL1PXqzijsZ3XAACs3YTFvtQ793.jpg "style=" float: none; "title=" Qq20140729-3.png "alt=" Wkiol1pxqzijsz3xaacs3ytfvtq793.jpg "/>


The following is the code to write, first in the View controller import:

#import <CoreLocation/CoreLocation.h> #import <MapKit/MapKit.h>

Two header files, and then the specific code in. m is as follows:


#import   "ANNViewController.h" @interface  ANNViewController  () @property   (strong,  nonatomic)  IBOutlet UILabel *longitude; @property   (strong, nonatomic)  iboutlet  UILabel *latitude; @property   (strong, nonatomic)  iboutlet uilabel *location ; @property   (strong, nonatomic)  CLLocationManager *locationManager; @end @implementation  ANNViewController-  (void) viewdidload{    [super viewdidload];// do  any additional setup after loading the view, typically from  a nib.    self.view.backgroundColor = [UIColor whiteColor];         //Creating Cllocationmanager Objects      self.locationmanager = [[cllocationmanager alloc] init];    //set proxy for yourself     self.locationmanager.delegate = self;    }-  (ibaction) Locationbutton: (uibutton *) sender {     [self.locationmanager startupdatinglocation];} -  (void) Locationmanager: (cllocationmanager *) Managerdidupdatetolocation: (cllocation *) Newlocation   fromlocation: (cllocation *) oldlocation{         //display longitude on label     self.longitude.text = [NSString  stringwithformat:@ "%lf",  newlocation.coordinate.longitude];    //latitude reality to label      self.latitude.text = [nsstring stringwithformat:@ "%LF",  newlocation.coordinate.latitude];        //  Get your current city name      CLGeocoder *geocoder = [[CLGeocoder alloc] init];     //the address information according to the latitude and longitude of the reverse geo-compilation     [geocoder reversegeocodelocation:newlocation completionhandler:^ (Nsarray *array, nserror *error)      {          if  (array.count > 0)           {              clplacemark *placemark = [array objectatindex:0];                            //all the information you get is displayed on the label               self.location.text = placemark.name;              //get the city              nsstring  *city = placemark.locality;              if  (!ciTY)  {                 The city information of  //Four municipalities cannot be obtained by locality, but only by obtaining the province's method (if it is empty, it is known as the municipality)                   city = placemark.administrativeArea;              }              nslog (@ "city = %@",  city);                        }         else if  (error == nil  && [array count] == 0)          {              nslog (@ "No results  were returned. ");   &nbSp;      }         else if   (Error != nil)          {              nslog (@ "an error occurred = %@",  error);         }     }];  The        //system will keep updating the data until you choose to stop the update, because we just need to get a latitude and longitude, so get it and stop the update      [manager stopupdatinglocation];}


The main city of municipalities to obtain the need to turn a corner, IOS7 added a new method, instead of the above method:

-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{NSLog (@ "longitude =%f"    , ((Cllocation *) [Locationslastobject]). Coordinate.longitude);        NSLog (@ "latitude =%f", ((Cllocation *) [Locations Lastobject]). Coordinate.latitude); [Manager stopupdatinglocation];}


After the treatment and the above method, we can look at.


There are also some Clgeocoder properties as follows:


@property   (nonatomic, readonly)  NSDictionary *addressDictionary;// address  dictionary properties@property  (nonatomic, readonly)  nsstring *name; // eg.  apple inc. @property   (nonatomic, readonly)  NSString *thoroughfare; //  street address, eg. 1 infinite loop@property  (nonatomic, readonly)   nsstring *subthoroughfare; // eg. 1@property  (nonatomic, readonly)  NSString  *locality; // city, eg. Cupertino@property  (nonatomic, readonly)   nsstring *sublocality; // neighborhood, common name, eg. mission  district@property  (nonatomic, readonly)  nsstring *administrativearea; // state,  eg. CA@property  (nonatomic, readonly)  nsstring *subadministrativearea; //  county, eg. santa clara@property  (nonatomic, readonly)  nsstring *postalcode; // zip  code, eg. 95014@property  (nonatomic, readonly)  NSString *ISOcountryCode;  // eg. US@property  (nonatomic, readonly)  NSString *country; //  eg. united states@property  (nonatomic, readonly)  nsstring *inlandwater; //  eg. Lake Tahoe@property  (nonatomic, readonly)  nsstring *ocean; //  eg. Pacific Ocean@property  (nonatomic, readonly)  NSArray *areasOfInterest;  // eg. golden gate park


The complete project is as follows:

Accessories: Https://github.com/winann/TestLocation.git


This article is from the "A Mao" blog, please be sure to keep this source http://winann.blog.51cto.com/4424329/1532528

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.