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