Jane_Meng
Jane_Meng

Reputation: 759

CLLocationManagerDelegate, when will the method be invoke: locationManager:didUpdateToLocation:fromLocation:

in CLLocationManagerDelegate, when will the method be invoke: locationManager:didUpdateToLocation:fromLocation:

could you explain more detail, if possible could you explain with example? thanks very much

  1. if my current location is changing(eg, I am on a train), does this method be invoked? if yes, how many times or how frequently it is be invoked?

  2. if I stay at one place and not move, does this method be invoked? if yes, how many times or how frequently it is be invoked?

Upvotes: 1

Views: 2789

Answers (2)

SJS
SJS

Reputation: 2645

For the first one this method will invoked, and the frequency depends on the speed.

And if don't change your location then this method will not invoked.

And how much time your location is changing this method will call.

Upvotes: 1

rich
rich

Reputation: 2136

This method is called whenever your iOS device has moved past the distance filter you have set. For example if you set it to

[self.locationManager setDistanceFilter:kCLDistanceFilterNone];

The method will be called every time the device is moved.

A code example of this would be finding the coordinates then assigning those values to labels

- (void)viewDidLoad{
[super viewDidLoad];
altitudeLabel.text = @"0 ft";
ftOrM = YES;
// Note: we are using Core Location directly to get the user location updates.
// We could normally use MKMapView's user location update delegation but this does not work in
// the background.  Plus we want "kCLLocationAccuracyBestForNavigation" which gives us a better accuracy.
//
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self; // Tells the location manager to send updates to this object
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];

[self.locationManager startUpdatingLocation];

}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:   (CLLocation*)newLocation fromLocation:(CLLocation *)oldLocation { 
tLatitude  = [NSString stringWithFormat:@"%3.5f", newLocation.coordinate.latitude]; 
tLongitude = [NSString stringWithFormat:@"%3.5f", newLocation.coordinate.longitude];
/*  the following returns 0 */
float distanceMeters;
float distanceFeet;
if (ftOrM == YES) {
    distanceMeters = newLocation.altitude;
    distanceFeet = distanceMeters * 3.2808399;
    tAltitude  = [NSString stringWithFormat:@"%.02f ft",    distanceFeet];
     altitudeLabel.text = tAltitude;
}
else {
tAltitude  = [NSString stringWithFormat:@"%.02f m",    newLocation.altitude];
NSLog(@"Altitude:");
NSLog(@"%@", tAltitude);
altitudeLabel.text = tAltitude;
NSLog(@"Altitude:");
NSLog(@"%@", tAltitude);
}
//[manager stopUpdatingLocation];
}

Upvotes: 4

Related Questions