didUpdateToLocation 停止调用

Posted

技术标签:

【中文标题】didUpdateToLocation 停止调用【英文标题】:didUpdateToLocation stop call 【发布时间】:2012-11-03 00:27:45 【问题描述】:

以下方法

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

打了 1 到 2 次就再也没有了……为什么?

 -(void) startLocation 
 if (locationManager == nil)
    locationManager = [[CLLocationManager alloc] init];

locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
locationManager.distanceFilter = 10;

[locationManager startUpdatingLocation];
  

和:

       - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
contadorSegundos++;
float diferencaLatitude = 0;
float diferencaLongitude = 0;
NSLog(@"old latitude: %f", oldLocation.coordinate.latitude);
NSLog(@"old longitude: %f", oldLocation.coordinate.longitude);


NSLog(@"new longitude: %f", newLocation.coordinate.longitude);
NSLog(@"old latitude: %f", newLocation.coordinate.latitude);

if(oldLocation) 
if(oldLocation.coordinate.latitude > newLocation.coordinate.latitude)
    diferencaLatitude = oldLocation.coordinate.latitude - newLocation.coordinate.latitude;
else if(newLocation.coordinate.latitude > oldLocation.coordinate.latitude)
    diferencaLatitude = newLocation.coordinate.latitude - oldLocation.coordinate.latitude;


if (oldLocation) 

if(oldLocation.coordinate.longitude > newLocation.coordinate.longitude)
    diferencaLongitude = oldLocation.coordinate.longitude - newLocation.coordinate.longitude;
else if(newLocation.coordinate.longitude > oldLocation.coordinate.longitude)
    diferencaLongitude = newLocation.coordinate.longitude - oldLocation.coordinate.longitude;


//NSLog(@"dif lat %f", diferencaLatitude);

// NSLog(@"dif long %f", diferencaLongitude);

CLLocationCoordinate2D coord;
if ((diferencaLatitude > 1) || (diferencaLongitude > 1)) 
    _emMovimento = YES;
    NSLog(@"em movimento");

else 
    if(contadorSegundos > 10) 
    _emMovimento = NO;
    contadorSegundos = 0;
        NSLog(@"parado");
    


coord.longitude = newLocation.coordinate.longitude;
coord.latitude = newLocation.coordinate.latitude;
_latitude = coord.latitude;
_longitude = coord.longitude;

NSLog(@"latitude: %f", _latitude);
NSLog(@"longitude: %f", _longitude);
MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:coord];
[geocoder setDelegate:self];
[geocoder start];

【问题讨论】:

【参考方案1】:

这个问题是因为ARC自动释放对象LocationManager。

【讨论】:

【参考方案2】:

此方法不再使用。您必须使用以下方法:

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

它返回一个位置数组,你得到最后一个。

[位置 lastObject]

【讨论】:

【参考方案3】:

当您使用下面的过滤器时,您是在告诉代表仅更新位置案例用户将其位置更改至少 10 米。 Ist that what you want? If its 不是,将desiredAccuracy 更改为最佳,将距离过滤器更改为none。

locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; locationManager.distanceFilter = 10;

【讨论】:

以上是关于didUpdateToLocation 停止调用的主要内容,如果未能解决你的问题,请参考以下文章

CLLocationManager 委托方法 didUpdateToLocation 没有被调用

didUpdateToLocation 调用了两次,好的。为啥 oldLocation 两次都为零?

一段时间后没有调用 CLLocationManager didUpdateToLocation

CLLocation didupdatetolocation 未调用

didUpdateToLocation 未在我的设备中使用 iOS 4 调用...?

我希望 func locationManager(manager: CLLocationManager, didUpdateToLocation 即使在应用程序被杀死后也能被调用?