CLLocationManagerDelegate,什么时候调用该方法:locationManager:didUpdateToLocation:fromLocation:

发布于 2024-11-27 10:04:05 字数 265 浏览 0 评论 0原文

在 CLLocationManagerDelegate 中,何时调用该方法: locationManager:didUpdateToLocation:fromLocation:

你能解释更多细节吗?如果可能的话,你能用例子解释一下吗?非常感谢

  1. 如果我当前的位置发生变化(例如,我在火车上),是否会调用此方法?如果是,调用多少次或频率?

  2. 如果我呆在一个地方不动,这个方法会被调用吗?如果是,调用了多少次或者频率?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唯憾梦倾城 2024-12-04 10:04:06

每当您的 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];
}

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];
}
七七 2024-12-04 10:04:06

对于第一个方法,将调用此方法,频率取决于速度。

如果不更改您的位置,则不会调用此方法。

以及你的位置发生变化多少时间这个方法就会调用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文