views:

661

answers:

3

Hello i used CLLoction manager to get location.but when ever i start my location then it gives me oldlocation which is store in device previously.i want to reset the location for this i used flag on first launch of application i used newlocation and call stopupdateinglocation and startupdatinglocation method.But oldlocation is not changed it show old location.

A: 

You can't clear the old location, but you can check the horizontalAccuracy to know if this is a good location (current) or something old and irrelevant.

oldlocation means if i start my application and GPS gets Coordination and after walk of 5KM i start my application once again and GPS oldlocation is previous location. i calculate distance in my application it show 5KM move but i start my application and not moved.
Use the timestamp.
+2  A: 

Every CLLocation has a timestamp property that you can use to filter all location updates that are too old for your usage.

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
 NSTimeInterval age = [newLocation.timestamp timeIntervalSinceNow];
 if(age < SOMECONSTANTINSECONDS)
 {
  //Use location
 }
}
racha
A: 

Hi, I am still having problems with this issue. I have this code in ViewDidAppear:

self.locationManager = [[CLLocationManager alloc] init]; 
locationManager.delegate = self; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];

Then in the delegate, I have:

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


NSLog(@"location timestamp: %@",newLocation.timestamp);
NSLog(@"location timestamp: %f",[newLocation.timestamp timeIntervalSinceReferenceDate]);
NSLog(@"current time: %f",[NSDate timeIntervalSinceReferenceDate]);
NSLog(@"difference between location timestamp and current time: %f",[NSDate timeIntervalSinceReferenceDate] - [newLocation.timestamp timeIntervalSinceReferenceDate]);
NSLog(@"NEW LOCATION: %f, %f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);


if ([NSDate timeIntervalSinceReferenceDate] - [newLocation.timestamp timeIntervalSinceReferenceDate] > 60){
    NSLog(@"is greater than 60");
    manager.delegate = nil; 
    [manager startUpdatingLocation];
    [manager stopUpdatingLocation];
    [manager autorelease];
    self.determinedLocation = newLocation;
}else{
    NSLog(@"is less than 60");
    self.determinedLocation = newLocation;
}

MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
geocoder.delegate = self; 
[geocoder start];
self.searchLocation = determinedLocation;

}

And this is what I get in the console:

2010-04-21 11:15:58.814 CultureNOW[233:207] location timestamp: 2010-04-19 09:47:43 -0400

2010-04-21 11:15:58.821 CultureNOW[233:207] location timestamp: 293377663.929194

2010-04-21 11:15:58.825 CultureNOW[233:207] current time: 293555758.825596

2010-04-21 11:15:58.830 CultureNOW[233:207] difference between location timestamp and 
current time: 178094.900609

2010-04-21 11:15:58.834 CultureNOW[233:207] NEW LOCATION: 33.435618, -111.998864

2010-04-21 11:15:58.838 CultureNOW[233:207] is greater than 60

For instance, you can see that everything is working properly, it notes the timestamp is greater than 60 seconds old, but then still doesn't update the location. What am I doing wrong? Thanks for any help, this is very frustrating.

David Giglio
Did you ever resolve this issue?
Nick