#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface NearByViewController : UIViewController <CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *latLbl;
@property (weak, nonatomic) IBOutlet UILabel *lngLbl;
@property (weak, nonatomic) IBOutlet UILabel *addressLbl;
@end
//-------------------------------------------------------
#import "NearByViewController.h"
@interface NearByViewController () {
CLLocationManager *locationManager;
}
@end
@implementation NearByViewController
-(void)initializeLocationManager {
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setDistanceFilter:100];
[locationManager startUpdatingLocation];
if([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
[locationManager requestWhenInUseAuthorization]; // Add This Line
}
NSLog(@"Location Manager is initialised");
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self initializeLocationManager];
}
#pragma mark - LocationManager Delegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
/*
if(currentLocation.latitude == 0.0 && currentLocation.longitude == 0.0) {
currentLocation = manager.location.coordinate;
NSLog(@"Latitude : %.2f, Longitude : %.2f", currentLocation.latitude, currentLocation.longitude);
}
*/
NSLog(@"Location manager didupdate in NearByViewController");
CLLocation *currentLocation = manager.location;
// NSLog(@"Current Location 11 : %f , %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
self.latLbl.text = [NSString stringWithFormat:@"%0.3f", currentLocation.coordinate.latitude];
self.lngLbl.text = [NSString stringWithFormat:@"%0.3f", currentLocation.coordinate.longitude];
// currentLocation = [locations lastObject];
// NSLog(@"Current Location 22 : %f , %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
[locationManager stopUpdatingLocation];
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:[locations objectAtIndex:0] completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
/*
isoCountryCode.text = placemark.ISOcountryCode;
country.text = placemark.country;
postalCode.text = placemark.postalCode;
adminArea.text = placemark.administrativeArea;
subAdminArea.text = placemark.subAdministrativeArea;
locality.text = placemark.locality;
subLocality.text = placemark.subLocality;
thoroughfare.text = placemark.thoroughfare;
subThoroughfare.text = placemark.subThoroughfare;
*/
NSString *address = [NSString stringWithFormat:@"%@\n%@ %@\n%@\n%@",
placemark.thoroughfare,
placemark.postalCode, placemark.locality,
placemark.administrativeArea,
placemark.country];
NSLog(@"Address : %@", address);
self.addressLbl.text = address;
}];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
NSLog(@"Location Manager did fail : %@", [error localizedDescription]);
}
#pragma mark -
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end