gpt4 book ai didi

javascript - 加载多个谷歌地图实例

转载 作者:行者123 更新时间:2023-11-28 21:24:29 25 4
gpt4 key购买 nike

这是目前我将 Google map 实例加载到 map div 中的方式。

我想添加另一个 ID 为 map2 的 div 来保存另一个 google map 实例,我该怎么做?

<script type="text/javascript"> 
var map;
function initialize() {
var myLatlng = new google.maps.LatLng(33.33333,44.44444);
var myOptions = {
zoom: 14,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map"), myOptions);


var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title:"If this is your exact location, press \"Add this location\""
});
google.maps.event.addListener(marker, 'click', function() {
map.setZoom(8);
});
}


</script>
<div id="map"></div>

最佳答案

如果您想通过 JavaScript 执行此操作

var map2 = document.createElement('div');
map2.id = "map2";

document.body.innerHTML += map2;

然后使用不同的div id调用初始化函数;,即map2;

编辑:您确定没有错误地调用该函数吗?

<script type="text/javascript"> 

function initialize(mapNum) {
var map;
var myLatlng = new google.maps.LatLng(33.33333,44.44444);
var myOptions = {
zoom: 14,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map"+mapNum), myOptions);


var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title:"If this is your exact location, press \"Add this location\""
});
google.maps.event.addListener(marker, 'click', function() {
map.setZoom(8);
});
}


initialize(1);
initialize(2);

</script>
<div id="map1"></div>
<div id="map2"></div>

关于javascript - 加载多个谷歌地图实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5398056/

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