Khant Thu Linn
Khant Thu Linn

Reputation: 6133

CLLocationCoordinate2D latitude & longitude always zero

I am trying to get latitude & longitude. I am running following code on actual device but I always get zero for latitude & longitude. I also import library and set delegate also. May I know what is wrong and how to do? My device has ios 8.

CLLocationManager *locationManager = [[CLLocationManager alloc] init];

if ([locationManager locationServicesEnabled])
{
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];
}


CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];

NSString *str=[[NSString alloc] initWithFormat:@" latitude:%f longitude:%f",coordinate.latitude,coordinate.longitude];
NSLog(@"%@",str);

Upvotes: 0

Views: 2675

Answers (1)

kabarga
kabarga

Reputation: 803

you have to get the current location in delegate method

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *currentLocation = [locations objectAtIndex:0];
}

Upvotes: 2

Related Questions