我正在尝试使用Google Map API。 它在本地计算机上完美运行。但是当我在实时服务器中部署代码时,它在控制台中显示错误,并且我无法加载Google Map
脚本:
<script
src="https://maps.googleapis.com/maps/api/js?key=*****&libraries=places&callback=initAutocomplete"
async defer></script>
function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map'), {
center : {
lat : 11.39930615709189,
lng : 79.69353675842285
},
zoom : 13,
mapTypeId : 'roadmap'
});
// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
document.getElementById('addAddress').addEventListener('click',
function() {
addAddress(geocoder, map, service);
});
var markers = [];
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = [];
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
var icon = {
url : place.icon,
size : new google.maps.Size(71, 71),
origin : new google.maps.Point(0, 0),
anchor : new google.maps.Point(17, 34),
scaledSize : new google.maps.Size(25, 25)
};
// Create a marker for each place.
markers.push(new google.maps.Marker({
map : map,
icon : icon,
title : place.name,
position : place.geometry.location
}));
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
var geocoder = new google.maps.Geocoder();
/* document.getElementById('submit').addEventListener('click', function() {
geocodeAddress(geocoder, map);
}); */
var service = new google.maps.DistanceMatrixService;
}
错误:
未捕获(承诺)的Xc消息:“ initAutocomplete不是 函数”名称:“ InvalideValueError”堆栈:“错误 在新的Xc上(https://maps.googleapis.com/maps/api/js?key= *****&libraries = places&callback = initAutocomplete:63:227) 在Object ._。Yc(https://maps.googleapis.com/maps/api/js?key= *****&libraries = places&callback = initAutocomplete:63:337) 在Uh(https://maps.googleapis.com/maps/api/js?key= *****&libraries = places&callback = initAutocomplete:132:221) 在https://maps.googleapis.com/maps/api/js?key= *****&libraries = places&callback = initAutocomplete:132:107“
答案 0 :(得分:0)
我遇到了同样的问题。 我在定义函数后加载了地图脚本。 现在它工作正常。