Sunday, February 16, 2014

Getting Current Location



.h file


    BOOL didFindLocation;

.m file

- (void)viewDidLoad
  [self startFindLocation];

-(void)startFindLocation
{
    self.didFindLocation=NO;
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];

}


#pragma mark Map Current Location=========================


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
//    UIAlertView *errorAlert = [[UIAlertView alloc]
//                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
//    [errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    
    CLLocation *currentLocation = newLocation;
    
    if (!self.didFindLocation)
    {
        self.didFindLocation = YES;
        [locationManager stopUpdatingLocation];

       self.longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
        self.latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];

       NSLog(@"strLongitude===%@",self.longitude);
        
        NSLog(@"strLatitude===%@",self.latitude);


    }
    
   

}

No comments:

Post a Comment