The Core Location Framework provide the Objective-C interface for obtaining information about current location and heading(direction in which device is pointing) of user. By using this framework we can configure and schedule delivery of location and heading event and can define geographic regions.
Get current location:
CLLocationManager class have ability to deliver current location and heading event. We can use this class for start and stop delivery of location and heading event. Its provide all event information through delegates methods(CLLocationManagerDelegate).
Here code snippet for getting current location
-(void) getCurrentLocation{
//Create an instance of CLLocationManager
CLLocationManager * locationManager = [[CLLocationManager alloc] init];
// Set delegate
locationManager.delegate = self;
// set accuracy
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//request for authorization
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
//start updating locations
[locationManager startUpdatingLocation];
}
Now define the delegate method for getting location inforamation
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
CLLocation *currentLocation = [locations lastObject];
NSLog(@"Location latitude - %f",currentLocation.coordinate.latitude);
NSLog(@"Location longitude - %f", currentLocation.coordinate.latitude);
}
// get error message to fetch location
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
}
Its enough for getting current location of user.
Run the app.
First its prompt a message for getting accessibility to use current location(This message will appear first time only). Click on allow button and we can see location info in console area.
Note: For running the application on simulator, we need to do some extra setting, because by default simulator location is set to 'None' and this is the reason we are getting error message instead of current location information.
Now select 'Location' under Debug menu option of simulator, and choose 'custom location'. We can see some value for latitude and longitude, it is the position of Apple or Apple store, we can change latitude and longitude value. Now click on 'Ok' and run the app again.
we can see latitude and longitude value on console area.
Now we are getting current location of user in the form of latitude and longitude, but is it enough to find address/location by a user? If i provide my latitude and longitude details to some one , is it possible to find my location for that?
We are getting current location information but its not understand for user. So we are going to convert location info in human readable format.
No need to worry about conversion because Core Location framework has ability to perform this task by CLGeocoder.
CLGeocoder:
The CLGeocoder class provides services for converting coordinate(latitude and longitude) to the user-friendly representation. A user-friendly representation of the coordinate typically consists of the street, city, state, and country information corresponding to the given location, but it may also contain a relevant point of interest, landmarks, or other identifying information.
Now back to code and update locationManager: didUpdateLocations: method with below code
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
//stop updating locations
[manager stopUpdatingLocation];
//get current location
CLLocation *currentLocation = [locations lastObject];
NSLog(@"Location latitude - %f",currentLocation.coordinate.latitude);
NSLog(@"Location longitude - %f", currentLocation.coordinate.latitude);
//convert coodinate to user-friendly representation
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:locations.lastObject completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *placeMark = [placemarks lastObject];
NSLog(@"name = %@", placeMark.name);
NSLog(@"address dict = %@",placeMark.addressDictionary);
}];
}
Run the application again.
Now we can see address details in console area.
Get current location:
CLLocationManager class have ability to deliver current location and heading event. We can use this class for start and stop delivery of location and heading event. Its provide all event information through delegates methods(CLLocationManagerDelegate).
Here code snippet for getting current location
-(void) getCurrentLocation{
//Create an instance of CLLocationManager
CLLocationManager * locationManager = [[CLLocationManager alloc] init];
// Set delegate
locationManager.delegate = self;
// set accuracy
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//request for authorization
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
//start updating locations
[locationManager startUpdatingLocation];
}
Now define the delegate method for getting location inforamation
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
CLLocation *currentLocation = [locations lastObject];
NSLog(@"Location latitude - %f",currentLocation.coordinate.latitude);
NSLog(@"Location longitude - %f", currentLocation.coordinate.latitude);
}
// get error message to fetch location
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
}
Its enough for getting current location of user.
Run the app.
First its prompt a message for getting accessibility to use current location(This message will appear first time only). Click on allow button and we can see location info in console area.
Note: For running the application on simulator, we need to do some extra setting, because by default simulator location is set to 'None' and this is the reason we are getting error message instead of current location information.
Now select 'Location' under Debug menu option of simulator, and choose 'custom location'. We can see some value for latitude and longitude, it is the position of Apple or Apple store, we can change latitude and longitude value. Now click on 'Ok' and run the app again.
we can see latitude and longitude value on console area.
Now we are getting current location of user in the form of latitude and longitude, but is it enough to find address/location by a user? If i provide my latitude and longitude details to some one , is it possible to find my location for that?
We are getting current location information but its not understand for user. So we are going to convert location info in human readable format.
No need to worry about conversion because Core Location framework has ability to perform this task by CLGeocoder.
CLGeocoder:
The CLGeocoder class provides services for converting coordinate(latitude and longitude) to the user-friendly representation. A user-friendly representation of the coordinate typically consists of the street, city, state, and country information corresponding to the given location, but it may also contain a relevant point of interest, landmarks, or other identifying information.
Now back to code and update locationManager: didUpdateLocations: method with below code
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
//stop updating locations
[manager stopUpdatingLocation];
//get current location
CLLocation *currentLocation = [locations lastObject];
NSLog(@"Location latitude - %f",currentLocation.coordinate.latitude);
NSLog(@"Location longitude - %f", currentLocation.coordinate.latitude);
//convert coodinate to user-friendly representation
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:locations.lastObject completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
CLPlacemark *placeMark = [placemarks lastObject];
NSLog(@"name = %@", placeMark.name);
NSLog(@"address dict = %@",placeMark.addressDictionary);
}];
}
Run the application again.
Now we can see address details in console area.
No comments:
Post a Comment