Tuesday, 21 February 2012

User Current Location Coordinates-iPhone

Add coreLocation frame work and then import
#import <CoreLocation/CoreLocation.h>     in the view the location cordinates is required.

//.h file

@interface HomeView : UIViewController<CLLocationManagerDelegate>{
  CLLocationManager *m_locationManager;
}

//.m file

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
   // [self.navigationController.navigationBar setAlpha:0.0];
   
    m_locationManager = [[CLLocationManager alloc] init];
    m_locationManager.delegate = self;
    m_locationManager.distanceFilter = kCLDistanceFilterNone;
    m_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [m_locationManager startUpdatingLocation];
}


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{   
    [manager stopUpdatingLocation];
    double lati = newLocation.coordinate.latitude+0.0;
    double longi = newLocation.coordinate.longitude+0.0;   
   
    NSNumber *lat = [NSNumber numberWithDouble:lati];
    NSNumber *lg = [NSNumber numberWithDouble:longi];
    NSLog(@"Current location-- %@-%@",lat,lg);
   
    [[NSUserDefaults standardUserDefaults] setObject:lat forKey:@"latitude"];
    [[NSUserDefaults standardUserDefaults] setObject:lg forKey:@"longitude"];
   
  }

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
   
    [manager stopUpdatingLocation];
       
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Device not Supported!" message:@""
                                                   delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
    [alert release];
   
    if(error != NULL)
        return;
}

No comments:

Post a Comment