0

下面的方法

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

3 回答 3

1

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

于 2012-11-03T22:17:42.397 回答
0

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

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

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

[地点 lastObject]

于 2012-11-03T01:39:37.867 回答
0

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

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

于 2012-11-03T02:29:46.563 回答