gpt4 book ai didi

javascript - Google map v3 : Markers and Polylines not rendering. 我的代码有什么问题?

转载 作者:行者123 更新时间:2023-12-03 08:09:34 25 4
gpt4 key购买 nike

问题:我尝试使用 Google map v3 将标记和折线添加到我的 map 中,但不起作用。它实际上没有渲染任何东西,我一直在关注 Google 文档,并且我有一个有效的 API key ,所以不知道...有建议吗?先感谢您!

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Simple Polylines</title>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>

function initMap() {
var myOptions = {
zoom: 4,
center: new google.maps.LatLng(42.0, 10.0),
mapTypeId: google.maps.MapTypeId.TERRAIN
}

var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var points = [
new google.maps.LatLng(39.0, -3.0),
new google.maps.LatLng(52.1, 12.1),
new google.maps.LatLng(40.2, 32.7)
];
var markers = [];
var path = [];
for (var i = 0; i < points.length; ++i) {
var marker = new google.maps.Marker({map: map, position: points[i]});
markers.push(marker);
path.push(marker.position);
}
var polyline = new google.maps.Polyline({
path: path,
strokeColor: "#FF0000"
});
polyline.setMap(map);
}

</script>

<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD6SfOqSpJQa45i0LWO-9zSrV9HDPzACYg&signed_in=true&callback=initMap"></script>

</body>
</html>

最佳答案

正如 Molle 博士在评论中所观察到的: map 容器的 ID 是 map ,而不是 map_canvas

    function initMap() {
var myOptions = {
zoom: 4,
center: new google.maps.LatLng(42.0, 10.0),
mapTypeId: google.maps.MapTypeId.TERRAIN
}

var map = new google.maps.Map(document.getElementById("map"), myOptions);
var points = [
new google.maps.LatLng(39.0, -3.0),
new google.maps.LatLng(52.1, 12.1),
new google.maps.LatLng(40.2, 32.7)
];
var markers = [];
var path = [];
for (var i = 0; i < points.length; ++i) {
var marker = new google.maps.Marker({map: map, position: points[i]});
markers.push(marker);
path.push(marker.position);
}
var polyline = new google.maps.Polyline({
path: path,
strokeColor: "#FF0000"
});
polyline.setMap(map);
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
    <div id="map"></div>
<script async defer
src="https://maps.googleapis.com/maps/api/js?signed_in=true&callback=initMap"></script>

关于javascript - Google map v3 : Markers and Polylines not rendering. 我的代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34202588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com