下面的方法
- (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];