Reputation: 1003
The following method
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;
call 1 or 2 times and never more...Why?
-(void) startLocation {
if (locationManager == nil)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
locationManager.distanceFilter = 10;
[locationManager startUpdatingLocation];
}
and :
- (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];
Upvotes: 0
Views: 598
Reputation: 1003
This problem was because the ARC release the object LocationManager automatically.
Upvotes: 1
Reputation: 416
When you use the filter below you are telling the delegate to only update the location case user change its location for at least 10 meters. Isnt that what you want? If it
s not, change the desiredAccuracy for best and the distance filter to none.
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
locationManager.distanceFilter = 10;
Upvotes: 0
Reputation: 416
This method is not in use anymore. You must use the method below:
It returns an array of locations, you get the last one.
[locations lastObject]
Upvotes: 0