在CLLocationManagerDelegate中,该方法何时被调用:locationManager:didUpdateToLocation:fromLocation:
你可以解释更多的细节,如果可能的话,你可以用例子来解释吗?非常感谢
如果我当前的位置正在改变(例如,我在火车上),是否会调用此方法?如果是,可以调用多少次或多少次?
如果我留在一个地方而不动,是否会调用此方法?如果是,可以调用多少次或多少次?
答案 0 :(得分:4)
只要您的iOS设备移过您设置的距离过滤器,就会调用此方法。例如,如果将其设置为
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
每次移动设备时都会调用该方法。
这样的代码示例是找到坐标,然后将这些值分配给标签
- (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];
}
答案 1 :(得分:1)
对于第一个调用此方法,频率取决于速度。
如果不改变您的位置,则不会调用此方法。
您的位置改变此方法的时间将会调用。