引子

大数据,云计算。。。这些词汇越来越多的出现在开发者和普通人的语境里,对于这些技术,不同的角度可以有不同的解读,但目的应该是统一明确的:存储分析利用过去的数据,当然是越多越好,也就是大数据,以最快的计算速度和最低的计算成本来组织、处理、分析这些数据(云计算),为解决当下的问题提供依据,如果还能够预测未来,让资源配置更优就更好了。

路线规划

在现实世界中,人、财、物时时刻刻都在流动,为了达到最优路径,路线规划就成了最本质最普适的计算需求。有需求就有相应的产品,以下是Google、百度、高德这三家地图市场占有率前三的路线规划功能:

google_nav_plan.png

baidu_nav_plan.png

amap_nav_plan.png

对比
相同点,三个地图中都有相应的路线规划功能,指定出发地点和到达地点,提供多条路径供选择。
不同点,在Google Maps中,对于路线规划多了指定出发时间的功能,在截图中箭头所指位置。使用Google Maps,指定未来时间,就可以回答:

  • 几点出发可以在下午3点前到达机场?
  • 我们的服务人员几点钟可以到达客户现场?
  • 下周二可以安排走访四个场地吗?
    。。。。。。

预测未来

指定出发时间,看似简单的一项功能,后边却包含了调用分析整个Google Maps累积的海量历史交通数据,利用Google的计算能力,在不影响API返回速度的提前下,对未来进行预测。

使用API

Google Maps API是Google Maps的技术支持,预测未来的这个功能,在API中也可以调用来让我们的应用,不论是LBS还是O2O,都变得更加智能。
可以在Direction和Matrix Distance中指定departure_time:
https://developers.google.com/maps/documentation/directions/intro
https://developers.google.com/maps/documentation/distance-matrix/intro
输入参数:departure_time,从1970/01/01,00:00:00开始的整数,单位是s。

Bonus

还有一个配置预测的参数,没有在API文档中列出来,那就是traffic_model,用来手工影响预测结果。
best_guess,默认值。
pessimistic
optimistic
一图胜千言:

traffic_mode_parameters

典型用途

企业级应用

ptt_in_ent

消费级应用

ptt_in_comsumer

使用授权

由于这个功能要消耗大量的计算资源,现在只在Google Maps API for Work中指定departure_time才会有效。

update

20151111
两个在线示例

http://mapsptt.appspot.com/getdirections

http://mapsptt.appspot.com/getdirections?origin=SFO,%20San%20Francisco,%20CA,%20United%20States&destination=Googleplex,%20Amphitheatre%20Parkway,%20Mountain%20View,%20CA,%20United%20States&client=gme-addictive&departure_time=1457254000

http://mapsptt.appspot.com/getdirections?origin=SFO,%20San%20Francisco,%20CA,%20United%20States&destination=Googleplex,%20Amphitheatre%20Parkway,%20Mountain%20View,%20CA,%20United%20States&client=gme-addictive&departure_time=1467254000

http://mapsptt.appspot.com/getdistance

http://mapsptt.appspot.com/getdistance?origin=SFO,%20San%20Francisco,%20CA,%20United%20States&destination=Googleplex,%20Amphitheatre%20Parkway,%20Mountain%20View,%20CA,%20United%20States&client=gme-addictive&departure_time=1457254000

http://mapsptt.appspot.com/getdistance?origin=SFO,%20San%20Francisco,%20CA,%20United%20States&destination=Googleplex,%20Amphitheatre%20Parkway,%20Mountain%20View,%20CA,%20United%20States&client=gme-addictive&departure_time=1467254000

20151113
另外两个示例

https://commutetraffic.appspot.com/ptt-directions.html

声明 direction service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Declare the Directions Service
var directionsService = new google.maps.DirectionsService();
/**
* Use the Places Autocomplete Place IDs for the origin and
* destination to reduce chances of errors.
* The departure time is in milliseconds and MUST be in the future.
* Travel mode is Driving.
*/
var request = {
origin: {
placeId: "ChIJyYfhZ79ZwokRMtXcL6CYxkA" }
},
destination: {
placeId: "ChIJvwJZrWH4wokRNBcFMQ0ohIE"
},
travelMode: google.maps.TravelMode.DRIVING,
drivingOptions: {
departureTime: new Date(1447621200000),
trafficModel: google.maps.TrafficModel.BEST_GUESS
}

发送请求处理回调

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
directionsService.route(request, function(result, status) {
// Check to see if the Service responded as expected
if (status == google.maps.DirectionsStatus.OK) {
var routesLegs = result.routes[0].legs[0];
var durationText = routesLegs.duration.text;
var durationInTrafficText = routesLegs.duration_in_traffic.text;
var duration = routesLegs.duration.value;
var durationInTraffic = routesLegs.duration_in_traffic.value;
// Create a "Traffic Factor" score between typical and predicted travel times
var trafficFactor = (durationInTraffic / duration);
} else {
// Alert why the Directions Service Request failed.
alert('Directions request failed due to ' + status);
}

返回结果中的 leg 数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
"legs": [
{
"duration": {
"text": "25 mins",
"value": 1471
},
"duration_in_traffic": {
"text": "28 mins",
"value": 1687
}
...

https://commutetraffic.appspot.com/ptt-distanceMatrix.html

和上个示例一样,使用 Distance Matrix API

参考

关于如何实现预测:
http://onlinepubs.trb.org/Onlinepubs/IDEA/FinalReports/Reliability/FINALREPORTL15A%20.pdf
http://people.orie.cornell.edu/woodard/WoodNogiKoch15.pdf