我正在尝试获取两个地点之间的交通时间。 我已遵循文档指南的所有详细信息,但无论交通模型如何,我总是得到两点之间的固定旅行时间:(best_guess,悲观或乐观)。 另外,我尝试将出发时间的值更改为 future 的不同日期,但我总是得到相同的结果。
编辑:
<小时/>我正在使用标准 API key 。 另外,我尝试更改 [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>
最佳答案
这似乎与您的代码无关。相反,这是一个许可问题。您正在使用Driving Options (BEST_GUESS、OPTIMISTIC、PESSIMISTIC)这是属于 Premium Plan clients 的功能。在我看来,您需要先购买许可证并使用高级客户端ID,然后才能使用它。
"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 API 高级计划的特殊功能。所有客户端 ID 均以 gme- 前缀开头。
此客户端 ID 不是 key 。它只能在您授权的 URL 上运行,因此您无需担心保密问题。
关于javascript - 从 Google Maps API for Javascript 获取 TravelTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37816001/