Now move to Coding part.
.h file
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface MyLocationController : UIViewController <CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
}
@end
.m file
- (void)viewDidLoad
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy =kCLLocationAccuracyNearestTenMeters;
//kCLLocationAccuracyBestForNavigation;// use high level of accuracy including additinal sensor data,this level of accuracy is intended solely to use when device is connected with exteranl power supply.
// kCLLocationAccuracyBest -- highest level of accuracy for device running on batter power
// kCLLocationAccuracyHundredMeters - accurate within 100 meter
// kCLLocationAccuracyKilometer - accurate within km
// kCLLocationAccuracyNearestTenMeters - accurate within 10 meter
// kCLLocationAccuracyThreeKilometers - accurate within 3 km
//-------------------
locationManager.distanceFilter = 30.48f; //mts for 100 feet
[locationManager startUpdatingLocation];
}
// for ios5 it is deprecated in ios6
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
AppDelegate *appD = [[UIApplication sharedApplication] delegate];
Location *objLocation = [NSEntityDescription insertNewObjectForEntityForName:tblLocation inManagedObjectContext:appD.managedObjectContext];
//NSLog(@"%@",[NSString stringWithFormat:@"%f", currentLocation.coordinate.latitude]);
objLocation.latitude = [NSString stringWithFormat:@"%f", currentLocation.coordinate.latitude];
objLocation.longitude = [NSString stringWithFormat:@"%f", currentLocation.coordinate.longitude];
objLocation.timeStamp = [dateFormater stringFromDate: [NSDate date]];
NSLog(@"latittude:%@,longitude%@",objLocation.latitude,objLocation.longitude);
}
}
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
NSLog(@"Enter Region");
}
-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
NSLog(@"Start Monitoring Region");
}
-(void)locationManagerDidResumeLocationUpdates:(CLLocationManager *)manager
{
NSLog(@"Resume Location update");
}
// Change set ios6 location manager above is depreciated-------
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *curr=[locations lastObject];
// NSDate *eventDate=curr.timestamp;
// NSTimeInterval howEvent=[eventDate timeIntervalSinceNow];
// if(abs(howEvent) <15.0)
// {
if(curr!=nil)
{
AppDelegate *appD = [[UIApplication sharedApplication] delegate];
Location *objLocation = [NSEntityDescription insertNewObjectForEntityForName:tblLocation inManagedObjectContext:appD.managedObjectContext];
//NSLog(@"%@",[NSString stringWithFormat:@"%f", currentLocation.coordinate.latitude]);
objLocation.latitude = [NSString stringWithFormat:@"%f", curr.coordinate.latitude];
objLocation.longitude = [NSString stringWithFormat:@"%f", curr.coordinate.longitude];
objLocation.timeStamp = [dateFormater stringFromDate: [NSDate date]];
lat=curr.coordinate.latitude;
lon=curr.coordinate.longitude;
}
// }
NSLog(@"%f,%f",curr.coordinate.longitude,curr.coordinate.latitude);
}