为什么我的Google Maps API请求在服务器上运行时不起作用
问题描述:
我正试图找到一种方法来计算从一个位置到另一个位置的流量距离。我发现了一个在我的浏览器中完美运行的脚本,但是当我将它上传到我的Web服务器并打开它时。为什么我的Google Maps API请求在服务器上运行时不起作用
它不起作用。
我得到了谷歌的一个关键,但不知道如何将其添加到脚本。
下面是实际的代码
var directionsService = new google.maps.DirectionsService();
var request = {
origin : 'amsterdam',
destination : 'rome',
travelMode : google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
var output = response.routes[0].legs[0].distance.value;
document.getElementById("dump").innerHTML = output;
}
else {
}
});
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<p id="dump"></p>
答
您可以在URL中指定的查询字符串的Google Maps API
键,在src
属性script
标签:
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=YOUR_API_KEY"></script>