2016-04-18 154 views
-1

请告诉我如何计算从谷歌距离矩阵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" 
     } 
    } ] 
    } ] 
} 
+1

转换为通用单元并执行添加。 'NSTimeInterval'特别适合这项任务。 – Avi

+0

查看“日历计算”下的Xcode文档。有很多方法可以帮助你解决这个问题。 –

回答

3

从数据您发布它看起来像服务器提供在文本格式,并作为秒计数的时间差:(6小时48分是24480秒)

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

以秒为单位处理间隔比将时间间隔字符串转换回数字时间间隔要容易得多。只需获取“值”键的值并将这些值一起添加即可。 (这些值看起来是与字符串时间间隔相匹配的秒数。)

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

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

-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; 
} 

然后总结一下。 我希望它能帮助你。

+1

这并没有解决OPs问题。 –

0

在这里,您还可以使用简单函数从Google距离矩阵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; 
} 

希望这可以帮助你。

相关问题