![](/img/trans.png)
[英]Why the Google Maps API does not work on a server? [Error: The Geolocation service failed]
[英]Why does my Google Maps API Request not work when I run it on a server
我正在嘗試找到一種方法來計算從一個位置到另一位置的交通距離。 我找到了一個可以在瀏覽器中完美運行的腳本,但是當我將其上載到Web服務器然后將其打開時。
這是行不通的。
我從Google那里獲得了一個密鑰,但不知道如何將其添加到腳本中。
這是實際的代碼
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>
您可以在script
標簽的src
屬性的URL查詢字符串中指定Google Maps API
密鑰:
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=YOUR_API_KEY"></script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.