gpt4 book ai didi

javascript - 如何将谷歌地图定位到特定位置?

转载 作者:太空宇宙 更新时间:2023-11-03 23:36:11 24 4
gpt4 key购买 nike

我在三个不同的 div 上有三个 map Canvas 。在 javascript 中,我在所有三个中都将中心设置为相同的位置。但是第二个和第三个没有得到那个中心。可能是什么问题呢?有什么想法吗?

var centerPosition=new google.maps.LatLng(6.924782, 79.863643);
function initialize() {
var map_canvas1 = document.getElementById('map_canvas1');
var map_canvas2 = document.getElementById('map_canvas2');
var map_canvas3 = document.getElementById('map_canvas3');
var map_options1 = {
center: centerPosition,
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map_options2 = {
center: centerPosition,
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map_options3 = {
center: centerPosition,
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map1 = new google.maps.Map(map_canvas1, map_options1);
map2 = new google.maps.Map(map_canvas2, map_options2);
map3 = new google.maps.Map(map_canvas3, map_options3);

}
google.maps.event.addDomListener(window, 'load', initialize);

最佳答案

Please refer to the jsfiddle. Your code is working fine
http://jsfiddle.net/2mw6b/
var centerPosition=new google.maps.LatLng(6.924782, 79.863643);
function initialize() {
var map_canvas1 = document.getElementById('map_canvas1');
var map_canvas2 = document.getElementById('map_canvas2');
var map_canvas3 = document.getElementById('map_canvas3');
var map_options1 = {
center: centerPosition,
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map_options2 = {
center: centerPosition,
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map_options3 = {
center: centerPosition,
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map1 = new google.maps.Map(map_canvas1, map_options1);
map2 = new google.maps.Map(map_canvas2, map_options2);
map3 = new google.maps.Map(map_canvas3, map_options3);

}
initialize();

**Html code**
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v3&libraries=places,drawing&sensor=false"></script>
<div id="map_canvas1" class="mapCont"></div>
<div id="map_canvas2" class="mapCont"></div>
<div id="map_canvas3" class="mapCont"></div>
**Css**
.mapCont{
height: 200px;
width: 200px;
}

关于javascript - 如何将谷歌地图定位到特定位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24051961/

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