我编写了一段JavaScript代码。我想在使用Google Maps API V3的请求中使用durationInTraffic
。此功能仅适用于Google Maps for Work客户。
Here它说地图工作客户应该在他们的请求中包含他们的clientId和签名。但是我在哪里确实将clientId和签名放在我的请求中?以下是我的代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?"></script>
</head>
<body style="font-family: Arial; font-size: 13px; color: red;">
<form>
<input type="text" name="start" id="start" placeholder="Start"> --> <input type="text" name="ziel" id="ziel" placeholder="Ziel"><br>
<br>
<input type="text" name="stunde" id="stunde" placeholder="Stunde">:
<input type="text" name="minute" id="minute" placeholder="Minute">
<br>
<input type="text" name="tag" id="tag" placeholder="Tag">-
<input type="text" name="monat" id="monat" placeholder="Monat">-
<input type="text" name="jahr" id="jahr" placeholder="Jahr">
</form>
<input type="submit" value="Los!" onClick="getRoute()">
<div id="durationWithTraffic">Dauer mit Vekehr: </div>
<div id="durationWithoutTraffic">Dauer ohne Vekehr: </div>
<br>
<script type="text/javascript">
function getRoute(){
var directionsService = new google.maps.DirectionsService();
var myOptions = {
zoom:7,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var startValue=document.getElementById('start').value;
var zielValue=document.getElementById('ziel').value;
var stundeValue=document.getElementById('stunde').value;
var minuteValue=document.getElementById('minute').value;
var tagValue=document.getElementById('tag').value;
var monatValue=document.getElementById('monat').value;
var jahrValue=document.getElementById('jahr').value;
//departure time in Epoch time
var abfahrtsZeitValue=monatValue+"/"+tagValue+"/"+jahrValue+" "+stundeValue+":"+minuteValue;
var request_withTraffic = {
origin: startValue,
destination: zielValue,
travelMode: google.maps.DirectionsTravelMode.DRIVING,
transitOptions: {
departureTime: new Date(abfahrtsZeitValue)
},
durationInTraffic: true
};
var request_withoutTraffic = {
origin: startValue,
destination: zielValue,
travelMode: google.maps.DirectionsTravelMode.DRIVING,
transitOptions: {
departureTime: new Date(abfahrtsZeitValue)
},
durationInTraffic: false
};
directionsService.route(request_withTraffic, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
// Display the duration:
document.getElementById('durationWithTraffic').innerHTML="Dauer mit Vekehr: ";
document.getElementById('durationWithTraffic').innerHTML +=
response.routes[0].legs[0].duration.value + " seconds";
}
});
directionsService.route(request_withoutTraffic, function(response, status) {
if (status == google.maps.DirectionsStatus.OK)
{
document.getElementById('durationWithoutTraffic').innerHTML="Dauer ohne Verkehr: ";
document.getElementById('durationWithoutTraffic').innerHTML +=
response.routes[0].legs[0].duration.value + " seconds";
}
});
}
</script>
</body>
</html>
答案 0 :(得分:2)
您必须在调用脚本时提供客户端ID和签名,
这是您的地图脚本
<script src="https://maps.googleapis.com/maps/api/js?client=YOUR_CLIENT_ID&v=3.17&signature=base64signature"></script>
供参考
https://developers.google.com/maps/documentation/business/clientside/#client_id https://developers.google.com/maps/documentation/business/webservices/auth#signature_examples