Arpit Raniwala
Arpit Raniwala

Reputation: 235

couldn't find latitude and longitude of my iphone

i am trying to find out iphone's current location and for this I am using this method. and try to save it for further procedure. but when I print value of location in form of latitude and longitude It always giving me 0.00000 and 0.0000. I am not getting the point that where I am wrong.this method is save in GetLocation class.

-(void)updatelocation{

NSLog(@"coming in update location    in get location class");
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];

NSLog(@"location manager   %@", locationManager);
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];

NSLog(@"Latitude is: %f\n\n\n and longitude is: %f\n\n\n", location.coordinate.latitude, location.coordinate.longitude);
NSLog(@"coordinate   %f",coordinate.latitude);
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:latitude forKey:@"latitude"];
[defaults setObject:longitude forKey:@"longitude"];

}

when I call this method in another class like this.

GetLocation *location = [GetLocation alloc] init ];
[location updatelocation];

but I always get 0.0000 and 0.0000. can anybody tell me that where I am wrong.

Upvotes: 1

Views: 632

Answers (2)

Master Stroke
Master Stroke

Reputation: 5128

Look at the -[CLLocation coordinate] documentation first...and even this one...

Upvotes: 2

Scott Berrevoets
Scott Berrevoets

Reputation: 16946

You are going too fast, the CLLocationManager hasn't determined your location yet. You should implement CLLocationManagerDelegate's locationManager:didUpdateToLocation:fromLocation: method. In that method, the user's location is known.

Upvotes: 3

Related Questions