satheesh kumar
satheesh kumar

Reputation: 113

Get current latitude and longitude from didupdatelocation delegate method

I created a MAPVIEW to get latitude and longitude of user's current location.

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

The didUpdateLocations delegate method gives an array which has current location's latitude and longitude.
How do i get latitude and longitude from that array?

Upvotes: 0

Views: 1345

Answers (5)

iPhone developer.
iPhone developer.

Reputation: 2132

You need to implement below method of delegate to get current location of user.

(void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {

}

Upvotes: 0

Vishnuvardhan
Vishnuvardhan

Reputation: 5107

You need to get the last object from the locations array and then grab the lattitude and longitude by the way.

CLLocation *location  = [locations lastObject];
    CLLocationDegrees lattitude = location.coordinate.latitude;
    CLLocationDegrees longitude = location.coordinate.longitude;

Upvotes: 0

Hiren Varu
Hiren Varu

Reputation: 1477

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation * currentLocation = [locations lastObject];
NSString * latitude = [NSString stringWithFormat:@"%.6f", currentLocation.coordinate.latitude];
NSString * longitude = [NSString stringWithFormat:@"%.6f", currentLocation.coordinate.longitude];
}

Upvotes: 0

Quentin Hayot
Quentin Hayot

Reputation: 7876

In the locations array, you will find CLLocation objects. From there, you can get the coordinates like this:

CLLocation *location = [locations objectAtIndex:someIndex]; //You can have more than one update so loop through it.
CLLocationDegrees latitude = location.coordinate.latitude;
CLLocationDegrees longitude = location.coordinate.longitude;

Upvotes: 1

Apurv
Apurv

Reputation: 17186

The delegate method will return the array of CLLocation objects. This array contains most recent location at end of array.

So, you should receive it like this:

CLLocation *loc = [locations lastObject];

To access latitude, longitude write below code.

CLLocationDegrees latitude = loc.coordinate.latitude;
CLLocationDegrees longitude = loc.coordinate.longitude;

Upvotes: 1

Related Questions