Let's find out the latitude/longitude first to find out the address.
1) Add CoreLocation.framework under Targets -> Build Phases -> Link Binary With Libraries
2) Import in your view controller where you need to find out the address.
3) Add CLLocationManagerDelegate to Interface(.h file) of your view controller.
4) Create objects in Interface(.h file) like below
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLGeocoder *geoCoder;
5) Now in implementation(.m file) class do the following-
Call below method in ViewDidLoad.
- (void) findLocation{
_geoCoder = [[CLGeocoder alloc] init];
_locationManager = [[CLLocationManager alloc]init];
_locationManager.delegate = self;
_locationManager.distanceFilter = kCLDistanceFilterNone;
_locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[_locationManager startUpdatingLocation];
}
Add delegate method from CLLocationManagerDelegate.
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{ //Stop updating location to save battery power
[_locationManager stopUpdatingLocation];
[self getLocationName];
}
Add method getLocationName to get the address
- (void)getLocationName{
[_geoCoder reverseGeocodeLocation:_locationManager.location completionHandler:
^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
//String to find out any value from complete address
NSString *state = [placemark.addressDictionary valueForKey:@"State"] ;
NSLog(@"State name is: %@",state);
NSString *city = [placemark.addressDictionary valueForKey:@"City"] ;
NSLog(@"City name is: %@",city);
// complete address
NSLog(@"located at %@",placemark.addressDictionary);
}];
}
Output:
State name is: Uttarakhand
City name is: Dehra Dun
located at {
City = "Dehra Dun";
Country = India;
CountryCode = IN;
FormattedAddressLines = (
"National Highway 72",
"Dehra Dun",
Uttarakhand,
India
);
Name = "National Highway 72";
State = Uttarakhand;
Street = "National Highway 72";
Thoroughfare = "National Highway 72";
}
0 Comment(s)