のコードのこの部分はCLLocationManager
、移動距離を計算するために使用されます。ただし、を使用した後でもロケーションキャッシュは削除されません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;
}
このコードは機能しません。追跡を開始すると、最後に追跡を停止した場所から距離が計算されます。
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
NSLog(@"The location age %f",locationAge);
if (locationAge > 2.0)