IOS cainiao learning-Get current location and geographical information

Source: Internet
Author: User

In iOS development, to obtain the current location, you only need to take the following steps:
First, import CoreLocation framework to the project and reference
[Plain]
1. # import <CoreLocation/CoreLocation. h>
Then, declare the proxy method,
[Plain] view plaincopy
1. @ interface LocationManager: NSObject <CLLocationManagerDelegate>
Then, update the location information:
[Plain]
1. locationManager = [[CLLocationManager alloc] init];
2. locationManager. delegate = self;
3. locationManager. desiredAccuracy = kCLLocationAccuracyBest;
4. locationManager. distanceFilter = kCLDistanceFilterNone;
5. [locationManager startUpdatingLocation];
LocationManager must be defined as a private or attribute variable. After each location update, call the CLLocationManagerDelegate-(void) locationManager :( CLLocationManager *) manager didUpdateToLocation :( CLLocation *) newLocation fromLocation :( CLLocation *) oldLocation method. newLocation in the method is the current location.
After obtaining the location, if you want to obtain the geographic information of the current location, you need to use CLGeocoder (MKReverseGeocoder is outdated ).
[Plain]
1. CLGeocoder * geocoder = [[CLGeocoder alloc] init];
2. [geocoder reverseGeocodeLocation: newLocation completionHandler: ^ (NSArray * array, NSError * error ){
3. if (array. count> 0 ){
4. CLPlacemark * placemark = [array objectAtIndex: 0];
5. NSString * country = placemark. ISOcountryCode;
6. NSString * city = placemark. locality;
7 .}
8.}];
Placemark contains the geographical information of newLocation.

 

 

From soloterry's column

Related Article

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.