从 Google 地图 API 获取 TravelTime for Javascript
Getting TravelTime from Google Maps API for Javascript
我正在尝试通过两个地点之间的交通情况获取行程时间。
我已遵循文档指南的所有详细信息,但无论交通模型如何,我总是在两点之间获得固定的旅行时间:(best_guess,悲观或乐观)。
另外,我试图将出发时间的值更改为将来的不同日期,但我总是得到相同的结果。
编辑:
我使用的是标准 API 密钥。
另外,我曾尝试更改 [departureTime:] 的日期格式,但没有帮助。
我的主要目标是获取路段上的行程时间。
感谢您的帮助。
这是我的 HTML 和 Javascript 代码:
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Displaying text directions with <code>setPanel()</code></title>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
#floating-panel {
position: absolute;
top: 10px;
left: 25%;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
text-align: center;
font-family: 'Roboto','sans-serif';
line-height: 30px;
padding-left: 10px;
}
#right-panel {
font-family: 'Roboto','sans-serif';
line-height: 30px;
padding-left: 10px;
}
#right-panel select, #right-panel input {
font-size: 15px;
}
#right-panel select {
width: 100%;
}
#right-panel i {
font-size: 12px;
}
#right-panel {
height: 100%;
float: right;
width: 390px;
overflow: auto;
}
#map {
margin-right: 400px;
}
#floating-panel {
background: #fff;
padding: 5px;
font-size: 14px;
font-family: Arial;
border: 1px solid #ccc;
box-shadow: 0 2px 2px rgba(33, 33, 33, 0.4);
display: none;
}
@media print {
#map {
height: 500px;
margin: 0;
}
#right-panel {
float: none;
width: auto;
}
}
</style>
</head>
<body>
<div id="floating-panel">
<strong>Get TravelTime in Traffic</strong>
</div>
<div id="right-panel"></div>
<div id="map"></div>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=[API_KEY]&callback=initMap">
</script>
<script>
function initMap() {
var directionsDisplay = new google.maps.DirectionsRenderer;
var directionsService = new google.maps.DirectionsService;
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: {lat: 30.114114, lng: 31.420595}
});
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('right-panel'));
var control = document.getElementById('floating-panel');
control.style.display = 'block';
map.controls[google.maps.ControlPosition.TOP_CENTER].push(control);
calculateAndDisplayRoute(directionsService, directionsDisplay);
}
function calculateAndDisplayRoute(directionsService, directionsDisplay) {
directionsService.route({
origin: "11 The Ring Road, Al Khosous, Al Khankah, Al Qalyubia Governorate, Egypt",
destination: " El-Shams Sporting Club, Al Matar, Qism El-Nozha, Cairo Governorate, Egypt",
travelMode: google.maps.TravelMode.DRIVING,
drivingOptions: {
departureTime: new Date("June 14, 2016 11:13:00"),
trafficModel: google.maps.TrafficModel.PESSIMISTIC
}
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
</script>
</body>
</html>
可能是你的日期格式不正确?
尝试:
departureTime: new Date("2016-06-14T11:13:00");
看来这与您的代码无关。相反,这是一个许可问题。您正在使用 Driving Options (BEST_GUESS, OPTIMISTIC,PESSIMISTIC) which is a feature that belongs to Premium Plan clients。在我看来,您需要先购买许可证并使用 Premium clientID 才能使用它。
"You must supply a Google Maps APIs Premium Plan client ID when
loading the API if you want to include a drivingOptions field in the
DistanceMatrixRequest."
您的客户 ID
购买 Google Maps API 高级计划许可 后,您将收到一封来自 Google 的欢迎电子邮件,其中包含您的客户端 ID。 您的客户端 ID 用于访问 Google Maps APIs 高级计划 的特殊功能。所有客户端 ID 都以 gme- 前缀开头。
此客户端 ID 不是密钥。它仅适用于您授权的 URL,因此您无需担心保密问题。
我正在尝试通过两个地点之间的交通情况获取行程时间。 我已遵循文档指南的所有详细信息,但无论交通模型如何,我总是在两点之间获得固定的旅行时间:(best_guess,悲观或乐观)。 另外,我试图将出发时间的值更改为将来的不同日期,但我总是得到相同的结果。
编辑:
我使用的是标准 API 密钥。 另外,我曾尝试更改 [departureTime:] 的日期格式,但没有帮助。
我的主要目标是获取路段上的行程时间。
感谢您的帮助。
这是我的 HTML 和 Javascript 代码:
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Displaying text directions with <code>setPanel()</code></title>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
#floating-panel {
position: absolute;
top: 10px;
left: 25%;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
text-align: center;
font-family: 'Roboto','sans-serif';
line-height: 30px;
padding-left: 10px;
}
#right-panel {
font-family: 'Roboto','sans-serif';
line-height: 30px;
padding-left: 10px;
}
#right-panel select, #right-panel input {
font-size: 15px;
}
#right-panel select {
width: 100%;
}
#right-panel i {
font-size: 12px;
}
#right-panel {
height: 100%;
float: right;
width: 390px;
overflow: auto;
}
#map {
margin-right: 400px;
}
#floating-panel {
background: #fff;
padding: 5px;
font-size: 14px;
font-family: Arial;
border: 1px solid #ccc;
box-shadow: 0 2px 2px rgba(33, 33, 33, 0.4);
display: none;
}
@media print {
#map {
height: 500px;
margin: 0;
}
#right-panel {
float: none;
width: auto;
}
}
</style>
</head>
<body>
<div id="floating-panel">
<strong>Get TravelTime in Traffic</strong>
</div>
<div id="right-panel"></div>
<div id="map"></div>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=[API_KEY]&callback=initMap">
</script>
<script>
function initMap() {
var directionsDisplay = new google.maps.DirectionsRenderer;
var directionsService = new google.maps.DirectionsService;
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: {lat: 30.114114, lng: 31.420595}
});
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('right-panel'));
var control = document.getElementById('floating-panel');
control.style.display = 'block';
map.controls[google.maps.ControlPosition.TOP_CENTER].push(control);
calculateAndDisplayRoute(directionsService, directionsDisplay);
}
function calculateAndDisplayRoute(directionsService, directionsDisplay) {
directionsService.route({
origin: "11 The Ring Road, Al Khosous, Al Khankah, Al Qalyubia Governorate, Egypt",
destination: " El-Shams Sporting Club, Al Matar, Qism El-Nozha, Cairo Governorate, Egypt",
travelMode: google.maps.TravelMode.DRIVING,
drivingOptions: {
departureTime: new Date("June 14, 2016 11:13:00"),
trafficModel: google.maps.TrafficModel.PESSIMISTIC
}
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
</script>
</body>
</html>
可能是你的日期格式不正确? 尝试:
departureTime: new Date("2016-06-14T11:13:00");
看来这与您的代码无关。相反,这是一个许可问题。您正在使用 Driving Options (BEST_GUESS, OPTIMISTIC,PESSIMISTIC) which is a feature that belongs to Premium Plan clients。在我看来,您需要先购买许可证并使用 Premium clientID 才能使用它。
"You must supply a Google Maps APIs Premium Plan client ID when loading the API if you want to include a drivingOptions field in the DistanceMatrixRequest."
您的客户 ID
购买 Google Maps API 高级计划许可 后,您将收到一封来自 Google 的欢迎电子邮件,其中包含您的客户端 ID。 您的客户端 ID 用于访问 Google Maps APIs 高级计划 的特殊功能。所有客户端 ID 都以 gme- 前缀开头。
此客户端 ID 不是密钥。它仅适用于您授权的 URL,因此您无需担心保密问题。