1

这部分代码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) 
4

4 回答 4

2

我使用一个计数器来计算调用didUpdateToLocation的次数。
我只使用至少 3 次通话后收到的位置。

我知道 3 是一个神奇的数字,但我发现前 3 个调用是缓存或非常不准确。

于 2012-10-14T09:10:43.813 回答
1

看看这个问题的答案,因为它包含来自 Apple 的示例应用程序“LocateMe”的代码,并提供了更多关于你想要做什么的细节:

https://stackoverflow.com/a/12848776/346098

于 2012-10-15T11:50:25.073 回答
0

您想timestamp在委托方法中检查新 CLLocation 对象的属性。听起来它只对不超过 XXX 秒的纬度/经度感兴趣。

NSTimeInterval timeInSeconds = [newLocation.timestamp timeIntervalSinceNow];
if (timeInSeconds > YOUR_CUSTOM_TIME_IN_SECONDS)
{
    // Do something
}
于 2012-10-14T09:20:28.840 回答
0

我找到了删除缓存的答案。第一次didUpdateToLocation调用,newlocation获取缓存值,old location为空。在第二次调用中,newlocation值被交换oldlocationnewlocation更新。因此,要获得更新的值,必须调用该函数两次。

- (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;
}
于 2012-10-17T14:16:11.473 回答