试图在一个页面上有几个 gmap,这些 gmap 位于隐藏的 div 中并被换出。第一张地图显示得很好,但所有后续地图在调用时都是部分灰色和偏离中心的。我看过很多这样的帖子,但没有一个解决方案有效 - 甚至 google.maps.event.trigger(map, "resize");
这就是我所拥有的。谢谢你的帮助。
function displayMap() {
document.getElementById('map_canvas').style.display="block";
initialize();}
function displayMap2() {
document.getElementById('map_canvas2').style.display="block";
initialize();}
function displayMap3() {
document.getElementById('map_canvas3').style.display="block";
initialize();}
function displayMap4() {
document.getElementById('map_canvas4').style.display="block";
initialize();
}
function initialize() {
var myLatlng = new google.maps.LatLng(39.924186, -75.297571);
var myLatlng2 = new google.maps.LatLng(40.152785, -76.750233);
var myLatlng3 = new google.maps.LatLng(39.962254, -75.605264);
var myLatlng4 = new google.maps.LatLng(40.152785, -76.750233);
var mapOptions = {
zoom: 13,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var mapOptions2 = {
zoom: 13,
center: myLatlng2,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var mapOptions3 = {
zoom: 13,
center: myLatlng3,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var mapOptions4 = {
zoom: 13,
center: myLatlng4,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
var map2 = new google.maps.Map(document.getElementById('map_canvas2'), mapOptions2);
var map3 = new google.maps.Map(document.getElementById('map_canvas3'), mapOptions3);
var map4 = new google.maps.Map(document.getElementById('map_canvas4'), mapOptions4);
var marker = new google.maps.Marker({
position: myLatlng,
map: map
});
var marker2 = new google.maps.Marker({
position: myLatlng2,
map: map2
});
var marker3 = new google.maps.Marker({
position: myLatlng3,
map: map3
});
var marker4 = new google.maps.Marker({
position: myLatlng4,
map: map4
});
}
google.maps.event.addDomListener(window, 'load', initialize);
交换 div 脚本
jQuery(function(){
jQuery('#showall').click(function(){
jQuery('.targetDiv').show();
});
jQuery('.showSingle').click(function(){
jQuery('.targetDiv').hide();
jQuery('#div'+$(this).attr('target')).show();
});
});
html
<div class="left_club" style="float:left; width:200px; margin-right:15px;">
<h3>Men's Clubs</h3>
<p><a class="showSingle" target="1" onclick="displayMap()">Royal Legends VBC</a><br>
<a class="showSingle" target="2" onclick="displayMap2()">Yorktowne VBC</a></p>
<h3>Women's Clubs</h3>
<p><a class="showSingle" target="3" onclick="displayMap3()">Lokahi</a><br>
<a class="showSingle" target="4" onclick="displayMap4()">Yorktowne VBC</a></p>
</div>
<div class="targetDiv" id="div1" style=" display:inline;">
<h2>Royal Legends VBC</h2>
<p>Contact | <a href="mailto:sample.com">sample</a><br>
Location | Clifton Heights, PA 19018<br>
Phone Number | sample<br>
Website | <a href="http://sample.com/" target="_blank">sample</a>
<div id="map_canvas"></div>
</div>
<div class="targetDiv" id="div2">
<h2>Yorktowne VBC</h2>
<p>Contact | <a href="mailto:sample.com">sample</a><br>
Location | Etters PA 17319<br>
Phone Number | sample<br>
Website | <a href="http://www.sample.com/" target="_blank">sample</a>
<div id="map_canvas2"></div>
</div>
<div class="targetDiv" id="div3">
<h2>Lokahi</h2>
<p>Contact | <a href="mailto:sample.net">sample</a><br>
Location | West Chester PA 19380<br>
Phone Number | sample<br>
<div id="map_canvas3"></div>
</div>
<div class="targetDiv" id="div4">
<h2>Yorktowne VBC</h2>
<p>Contact | <a href="mailto:sample.com">sample</a><br>
Location | Etters PA 17319<br>
Phone Number | sample<br>
Website | <a href="http://www.sample.com/" target="_blank">Yorktowne VBC</a>
<div id="map_canvas4"></div>
</div
的CSS
#map_canvas {margin-top:10px;width: 455px;height: 240px;float:left;border:1px solid #dcdcdc; margin-left:0px; margin-bottom:0px;}
#map_canvas2 {margin-top:10px;width: 455px;height: 240px;float:left;border:1px solid #dcdcdc; margin-left:0px; margin-bottom:0px;}
#map_canvas3 {margin-top:10px;width: 455px;height: 240px;float:left;border:1px solid #dcdcdc; margin-left:0px; margin-bottom:0px;}
#map_canvas4 {margin-top:10px;width: 455px;height: 240px;float:left;border:1px solid #dcdcdc; margin-left:0px; margin-bottom:0px;}
.showSingle {cursor:pointer;}
.targetDiv {float:left; width:458px; padding:20px;border:1px solid #dcdcdc; background-color:#f0efee;}
js在第一张地图后隐藏地图
jQuery(function(){
$('#div2, #div3, #div4').hide();
});