我试图让地图向我展示多条路线,但对我来说它只是显示空白页面,即使地图没有显示。我的代码中的问题是什么,我无法找到它。它只是空白页。
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title></title>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
float: left;
width: 63%;
height: 100%;
</style>
</head>
<body>
<div id="map"></div>
<script>
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: {lat: -24.345, lng: 134.46}
});
function renderDirections(result) {
var directionsRenderer = new google.maps.DirectionsRenderer({
draggable: true,
map: map,
});
directionsRenderer.setMap(map);
directionsRenderer.setDirections(result);
}
var directionsService = new google.maps.DirectionsService;
function requestDirections(start, end) {
directionsService.route({
origin: start,
destination: end,
travelMode: 'DRIVING'
}, function(result) {
renderDirections(result);
});
}
requestDirections('Huntsville, AL', 'Boston, MA');
requestDirections('Bakersfield, CA', 'Vancouver, BC');
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyA6POCSerSPAK1t9zhKiFbiTDzh5TDPeYg&callback=initMap">
</script>
</body>
</html>
答案 0 :(得分:1)
您忘记在代码中编写回调函数:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: {lat: -24.345, lng: 134.46}
});
function renderDirections(result) {
var directionsRenderer = new google.maps.DirectionsRenderer({
draggable: true,
map: map,
});
directionsRenderer.setMap(map);
directionsRenderer.setDirections(result);
}
var directionsService = new google.maps.DirectionsService;
function requestDirections(start, end) {
directionsService.route({
origin: start,
destination: end,
travelMode: 'DRIVING'
}, function(result) {
renderDirections(result);
});
}
requestDirections('Huntsville, AL', 'Boston, MA');
requestDirections('Bakersfield, CA', 'Vancouver, BC');
}
因为您在src中调用它但从未实现它:
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyA6POCSerSPAK1t9zhKiFbiTDzh5TDPeYg&callback=initMap"