CLLocationManager保存缓存值 [英] CLLocationManager holds cache value
问题描述
CLLocationManager
的这部分代码用于计算行进距离。但是即使使用 timeIntervalSinceNow
后也不会删除位置缓存。
This part of code of CLLocationManager
is used to calculate the distance travelled. But the location cache is not removed even after using timeIntervalSinceNow
.
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if(newLocation != nil && oldLocation != newLocation)
{
tempNewLocation = newLocation;
tempOldLocation = oldLocation;
}
NSLog(@"New Location Found");
NSLog(@"- Latitude: %f", newLocation.coordinate.latitude);
NSLog(@"- Longitude: %f", newLocation.coordinate.longitude);
NSLog(@"- Altitude: %f", newLocation.altitude);
NSLog(@"- Course: %f", newLocation.course);
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
NSLog(@"The location age %f",locationAge);
if (locationAge > 2.0)
{
}
else
{
if (tempOldLocation.coordinate.latitude == tempNewLocation.coordinate.latitude && tempNewLocation.coordinate.longitude == tempOldLocation.coordinate.longitude)
{
NSLog(@" Fix location found ");
}
else
{
if(tempNewLocation.coordinate.latitude == tempOldLocation.coordinate.latitude && tempNewLocation.coordinate.longitude == tempOldLocation.coordinate.longitude)
{
NSLog(@"First Time Location Update");
latitudeLongitude.text = [[ NSString alloc] initWithFormat:@"%g , %g", tempNewLocation.coordinate.latitude, tempNewLocation.coordinate.longitude];
totalDistance = 0;
distance.text = @"0 miles";
}
else if ([tempNewLocation distanceFromLocation:tempOldLocation] - tempNewLocation.horizontalAccuracy >= 0)
{
totalDistance += [tempNewLocation distanceFromLocation:tempOldLocation] - (tempNewLocation.horizontalAccuracy / 2);
}
else{
totalDistance += [tempNewLocation distanceFromLocation:tempOldLocation];
}
if (totalDistance < 0) {
distance.text = @"0 miles";
}
else
milesdistance=0.000621371192*totalDistance;
distance.text = [[ NSString alloc] initWithFormat:@"%.1f", milesdistance];
odometerreading.text = [NSString stringWithFormat:@"%09.1f", milesdistance];
mileagerate.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"savedstring"];
float mileagefloat=[self.mileagerate.text floatValue];
amount.text = [NSString stringWithFormat:@"%.2f",mileagefloat * milesdistance];
amountstatus.text=[NSString stringWithFormat:@"$%.2f",mileagefloat * milesdistance];
newnumber=totalDistance;
}
此代码无效对我来说,当我开始跟踪时,会从我上次停止跟踪的位置开始计算距离。
This code doesnt work for me, and when I start tracking, distance is calculated from the place where I last stopped the tracking.
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
NSLog(@"The location age %f",locationAge);
if (locationAge > 2.0)
推荐答案
我找到了删除缓存的答案。第一次调用 didUpdateToLocation
时, newlocation
获取缓存值,而 old location
为空。
在第二个调用中,将 newlocation
的值交换为 oldlocation
和 newlocation
已更新。因此,要获取更新的值,该函数必须被调用两次。
I found the answer for removing cache. For the first time didUpdateToLocation
is called, the newlocation
fetches the cache value, and the old location
is null.
And in the second call, newlocation
value is swapped to oldlocation
and the newlocation
is updated. And hence to get the updated value, the function has to be called twice.
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
static CLLocation *locationanalysis1;
NSLog(@"New Location Found");
NSLog(@"- Latitude: %f", newLocation.coordinate.latitude);
NSLog(@"- Longitude: %f", newLocation.coordinate.longitude);
NSLog(@"- Altitude: %f", newLocation.altitude);
NSLog(@"- Course: %f", newLocation.course);
NSDate *eventDate = newLocation.timestamp;
NSTimeInterval howRecent = -[eventDate timeIntervalSinceNow];
if (howRecent > maximumElapsedTimeForCachedLocation) {
locationanalysis1=newLocation;
return;
}
if((locationanalysis1.coordinate.latitude-oldLocation.coordinate.latitude)==0){
NSLog(@"Old Location in location analysis is %@",oldLocation);
return;
}
NSLog(@"New location accuracy %.0fm", newLocation.horizontalAccuracy);
if ((newLocation.horizontalAccuracy < 0) || (newLocation.horizontalAccuracy > 10)) return;
if(oldLocation!=NULL && newLocation!=NULL){
totalDistance += [newLocation distanceFromLocation:oldLocation];
}else return;
}
这篇关于CLLocationManager保存缓存值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!