Obtain the longitude and latitude of iOS and obtain the detailed address through reverse Geographic Encoding.
_ LocationManager = [[CLLocationManager alloc] init];
// Expected longitude
_ LocationManager. desiredAccuracy = kCLLocationAccuracyBest;
// Update about 100 meters
_ LocationManager. distanceFilter = 100;
// Certificate NSLocationAlwaysUsageDescription
If ([[UIDevice currentDevice] systemVersion]. doubleValue> 8.0) {// if iOS is of version 8.0 or later
If ([_ locationManager respondsToSelector: @ selector (requestAlwaysAuthorization)]) {// The requestAlwaysAuthorization method is available in the location management object.
// Run
[_ LocationManager requestAlwaysAuthorization];
}
}
_ LocationManager. delegate = self;
[_ LocationManager startUpdatingLocation];
// Obtain the longitude and latitude and detailed address
-(Void) locationManager :( CLLocationManager *) manager didUpdateLocations :( NSArray <CLLocation *> *) locations {
CLLocation * location = [locations lastObject];
NSLog (@ "latitude ===% g longpolling = % g", location. coordinate. latitude, location. coordinate. longpolling );
// Reverse geocode
CLGeocoder * clGeoCoder = [[CLGeocoder alloc] init];
CLLocation * cl = [[CLLocation alloc] initWithLatitude: location. coordinate. latitude longpolling: location. coordinate. longpolling];
[ClGeoCoder reverseGeocodeLocation: cl completionHandler: ^ (NSArray * placemarks, NSError * error ){
For (CLPlacemark * placeMark in placemarks ){
NSDictionary * addressDic = placeMark. addressDictionary;
NSString * state = [addressDic objectForKey: @ "State"];
NSString * city = [addressDic objectForKey: @ "City"];
NSString * subLocality = [addressDic objectForKey: @ "SubLocality"];
NSString * street = [addressDic objectForKey: @ "Street"];
NSLog (@ "city ===%@%%%@%@", state, city, subLocality, street );
[_ LocationManager stopUpdatingLocation];
}
}];
}