Core Location FrameWork:
Core framework provides an interface to get the user's location.But however user must first allow the app to use location services in order to get the details.
Things to do first:
1.Add Core location framework;
2.Import protocol-CoreLocationManagerDelegate;
3.Add 4 Labels & 4 Corresponding Textfields To Display-Latitude,Longitude,Altitude,Speed in the storyboard.
Design:
Drag & drop 4text fields Corresponding to their labels from object library .The textfield Border style is changed from default to bezel;
meanwhile,declare these in the .h file.
IBOutlet-is used to define a property of a UIComponent.
textfields are controls on which we will show location data.
Synthesize the above properties in .m file.
Now it's time to do the real work.
CLLocation Manager is responsible for the location data.
in ViewDidLoad,we then instantiate the CLLocation manager object which will be responsible for location data.the delegate is set to self and desired accuracy is set to best,the higher accuracies might take more devices resources,We then call start updating method of the location manager,by doing this we request location updates.
Next step is to import delegate methods of CLLocationManager property
Delegate Method1-Import didFailWithError;
Delegate Method2-DidUpdateLocations;
This method is called when the device has new location data and it will display on the corresponding textfields.didUpdateLocations method provides an (NSArray*)locations which contains the most recent updates,so the most recent location is the last object of NSArray.We then assign the lastLocation value to CLLocation object(current location)and we set the location data to the textfields.
Make sure you give right format.
Here,Current.location.latitude &longitude -belongs to CLLocationDegrees Class.
Altitude belongs to CLLocation Distance that's why it's of the format-@"%.0f m".
Speed-CLLocationSpeed-format-@"%.1f m/s"-because distance is measured in metre/seconds.
That's it,save and run the program.
Core framework provides an interface to get the user's location.But however user must first allow the app to use location services in order to get the details.
Things to do first:
1.Add Core location framework;
2.Import protocol-CoreLocationManagerDelegate;
3.Add 4 Labels & 4 Corresponding Textfields To Display-Latitude,Longitude,Altitude,Speed in the storyboard.
Design:
meanwhile,declare these in the .h file.
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate>
@interface ViewController : UIViewController <CLLocationManagerDelegate>
IBOutlet-is used to define a property of a UIComponent.
textfields are controls on which we will show location data.
Synthesize the above properties in .m file.
Now it's time to do the real work.
CLLocation Manager is responsible for the location data.
in ViewDidLoad,we then instantiate the CLLocation manager object which will be responsible for location data.the delegate is set to self and desired accuracy is set to best,the higher accuracies might take more devices resources,We then call start updating method of the location manager,by doing this we request location updates.
Next step is to import delegate methods of CLLocationManager property
Delegate Method1-Import didFailWithError;
Delegate Method2-DidUpdateLocations;
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error;
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
CLLocation *CurrentLocation=[locations lastObject];
Latitude.text=[NSString stringWithFormat:@"%.8f",CurrentLocation.coordinate.latitude];
Longitude.text=[NSString stringWithFormat:@"%.8f",CurrentLocation.coordinate.longitude];
Altitude.text=[NSString stringWithFormat:@"%.0f m",CurrentLocation.altitude];
Speed.text=[NSString stringWithFormat:@"%.1f m/s",CurrentLocation.speed];
}
This method is called when the device has new location data and it will display on the corresponding textfields.didUpdateLocations method provides an (NSArray*)locations which contains the most recent updates,so the most recent location is the last object of NSArray.We then assign the lastLocation value to CLLocation object(current location)and we set the location data to the textfields.
Here,Current.location.latitude &longitude -belongs to CLLocationDegrees Class.
Altitude belongs to CLLocation Distance that's why it's of the format-@"%.0f m".
Speed-CLLocationSpeed-format-@"%.1f m/s"-because distance is measured in metre/seconds.
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
UIAlertView *LocationUpdateFail=[[UIAlertView alloc]initWithTitle:@"ERROR" message:@"Sorry,Fail to get to get the updates.chk later" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[LocationUpdateFail show];
}
ReplyDeleteThank you sharing the info very well explained.
IOS App Development Training in Delhi