交换包含多个谷歌地图的多个div

时间:2013-11-11 17:43:09

标签: javascript jquery

尝试在隐藏div中的一个页面上有几个gmaps并换出。第一张地图显示正常,但所有后续地图显示为部分灰色且偏离中心。我已经看过许多帖子,但没有一个解决方案有效 - 甚至google.maps.event.trigger(地图,“调整大小”);

这就是我所拥有的。谢谢你的帮助。

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();
    });

1 个答案:

答案 0 :(得分:1)

我知道这个问题非常好,这是我评论中陈述的可能解决方案之一,

问题是,当您在具有宽度和高度的元素上启动谷歌地图时,在此之后将其设置为显示:无,然后再次显示:阻止(隐藏/显示)它将弹出地图。

现在在这个小提琴上,当显示另一个地图或任何地图被隐藏时,容器的宽度和高度不会受到影响,它们只会被移动, http://jsfiddle.net/LNYSE/

css,所有地图都初始定位在左侧,用户看不到。

.targetDiv {position:absolute;top:220px; width:458px;left:-999px; padding:20px;border:1px solid #dcdcdc; background-color:#f0efee;}

html,只有第一张地图可见,因为左:0

<div class="targetDiv activeYo" id="div1" style=" display:inline;left:0;">

这是js,您不需要使用动画功能,您也只能使用.css('left',0)

jQuery('.showSingle').click(function(){
    jQuery('.activeYo').animate({left:-999}).removeClass('activeYo');
    jQuery('#div'+$(this).attr('target')).animate({left:0}).addClass('activeYo');
});

没有动画http://jsfiddle.net/LNYSE/1/

欢呼声