P粉3842444732023-08-23 20:45:44
https://maps.googleapis.com/maps/api
不支持从前端JavaScript中的Web应用程序获取请求,以您的代码尝试使用它的方式。
相反,您必须使用支持的Google Maps JavaScript API,其客户端代码与您尝试的方式不同。一个距离矩阵服务示例如下:
<script> var service = new google.maps.DistanceMatrixService; service.getDistanceMatrix({ origins: [origin1, origin2], destinations: [destinationA, destinationB], travelMode: 'DRIVING', unitSystem: google.maps.UnitSystem.METRIC, avoidHighways: false, avoidTolls: false },… </script> <script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"> </script>
这里是使用Place Autocomplete API的示例使用Places库:
<script> function initMap() { var map = new google.maps.Map(document.getElementById('map'), { center: {lat: -33.8688, lng: 151.2195}, zoom: 13 }); ... map.controls[google.maps.ControlPosition.TOP_RIGHT].push(card); var autocomplete = new google.maps.places.Autocomplete(input); autocomplete.bindTo('bounds', map); var infowindow = new google.maps.InfoWindow(); var infowindowContent = document.getElementById('infowindow-content'); infowindow.setContent(infowindowContent); var marker = new google.maps.Marker({ map: map, anchorPoint: new google.maps.Point(0, -29) }); </script> <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initMap" async defer></script>
像这样使用Maps JavaScript API-通过使用