didUpdateToLocation停止调用

时间:2012-11-03 00:27:45

标签: objective-c ios ios4 core-location

以下方法

- (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];

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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

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

[locations lastObject]

答案 2 :(得分:0)

当您使用下面的过滤器时,您告诉代表仅更新位置用户用户将其位置更改至少10米。 Isn t that what you want? If it不是,将最佳的所需精度和距离过滤器更改为无。

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