-1

请告诉我如何计算从 google 的距离矩阵 api 返回的时间间隔总和 - 例如,我得到 2 天 15 小时或 5 小时 49 分钟或 41 分钟。那么我怎样才能总结所有的时间间隔。请看我的回复——

{
  "status": "OK",
  "origin_addresses": [ "Vancouver, BC, Canada", "Seattle, État de Washington, États-Unis" ],
  "destination_addresses": [ "San Francisco, Californie, États-Unis", "Victoria, BC, Canada" ],
  "rows": [ {
    "elements": [ {
      "status": "OK",
      "duration": {
        "value": 340110,
        "text": "3 jours 22 heures"
      },
      "distance": {
        "value": 1734542,
        "text": "1 735 km"
      }
    }, {
      "status": "OK",
      "duration": {
        "value": 24487,
        "text": "6 heures 48 minutes"
      },
      "distance": {
        "value": 129324,
        "text": "129 km"
      }
    } ]
  }, {
    "elements": [ {
      "status": "OK",
      "duration": {
        "value": 288834,
        "text": "3 jours 8 heures"
      },
      "distance": {
        "value": 1489604,
        "text": "1 490 km"
      }
    }, {
      "status": "OK",
      "duration": {
        "value": 14388,
        "text": "4 heures 0 minutes"
      },
      "distance": {
        "value": 135822,
        "text": "136 km"
      }
    } ]
  } ]
}
4

3 回答 3

3

从您发布的数据看来,服务器以文本格式和秒数提供时间差:

    "value": 24487,
    "text": "6 heures 48 minutes"

(6 小时 48 分是 24480 秒。)

以秒为单位处理间隔比尝试将时间间隔字符串转换回数字时间间隔要容易得多。只需获取“value”键的值并将这些值相加。(这些值似乎是与字符串时间间隔匹配的秒数。)

然后,您可以使用NSDateComponentsFormatter将时间间隔转换回显示字符串。

您也可以使用NSDateComponentsFormatter将您的时间间隔字符串转换为数字时间间隔,但您需要确保您获得正确的语言环境,然后您必须处理服务器的字符串格式和 iOS 之间的差异。

于 2016-04-18T11:32:18.233 回答
0

在这里,您还可以使用简单函数从 Google distance Matrix API 获取时间计算。

您也可以根据需要调整此方法。

- (NSString *)getTotalTimeFromGoogleDistanceAPI:(NSArray *)timeArray
{
    double days  = 0;
    double hours = 0;
    double mins  = 0;

    for (NSString *str in timeArray) {

        // Split string into array to get values at index
        NSArray *stringSplitterArray = [str componentsSeparatedByString:@" "];

        if ([str containsString:@"day"] && [str containsString:@"hour"]) {

            days  += [[stringSplitterArray objectAtIndex:0] integerValue];
            hours += [[stringSplitterArray objectAtIndex:2] integerValue];

        }else
            if ([str containsString:@"hour"] && [str containsString:@"min"])
            {
                hours += [[stringSplitterArray objectAtIndex:0] integerValue];
                mins  += [[stringSplitterArray objectAtIndex:2] integerValue];
            }
            else
                {
                    mins  += [[stringSplitterArray objectAtIndex:0] integerValue];
                }
    }

    // Check for hours -->> if its is greater than 24 then convert it into Day
    if (hours > 23) {
        double extractDay   =  floor(hours/24);
        days += extractDay;

        double extractHours = fmod(hours, 24);
        hours = extractHours;
    }

    // Check for mins -->> if its is greater than 60 then convert it into Hours
    if (mins > 59) {
        double extractHours   =  floor(mins/60);
        hours += extractHours;

        double extractMins = fmod(mins, 60);
        mins = extractMins;
    }

    // Calculate final time
    NSString *timeString;

    if (days == 0) {
        timeString = [NSString stringWithFormat:@"%g hours %g mins", hours , mins];
    }else
        if (days == 0 && hours == 0){
            timeString = [NSString stringWithFormat:@"%g mins", mins];
        }else
            {
                timeString = [NSString stringWithFormat:@"%g days %g hours %g mins", days, round(hours) , round(mins)];
            }

    return timeString;
}

希望这对你有帮助。!!

于 2016-04-19T06:58:07.710 回答
-2

创建一个名为 previousTime 的属性。

@property (nonatomic, strong) NSDate *previousTime;

使用此方法查找时差。

 - (NSTimeInterval)timeDifferenceSinceLastOpen 
 {
    if (!previousTime) self.previousTime = [NSDate date];
    NSDate *currentTime = [NSDate date];
    NSTimeInterval timeDifference =  [currentTime timeIntervalSinceDate:prevTime];
    self.prevTime = currentTime;
    return timeDifference;
}

并总结一下。我希望,它会帮助你。

于 2016-04-18T10:45:07.840 回答