【问题标题】:Google map not centered in jQuery tabs when inactive tab has display:none当非活动选项卡显示时,谷歌地图不在 jQuery 选项卡中居中:无
【发布时间】:2015-11-25 16:03:56
【问题描述】:

我正在尝试为我的房地产网站实施 GMap,其中每个属性都有自己的地图,该地图基于位置纬度和经度以及自定义标记图标。问题是初始化后地图没有以标记图标为中心。我也阅读了很多帖子,但任何解决方案都不适合我,所以如果有人有能力提供帮助,我会很高兴找到一个可行的解决方案。

这是我到目前为止所做的。

视图文件中的代码 - 我将数据属性用于动态位置 (LatLong) 值,这适用于我在属性管理中的自定义字段。

<div class="property">
<ul class="tabs clearfix">
    <li class="current">Tab 1</li>
    <li>Tab 2</li>      
    <li>Tab 3</li>          
</ul>
<div class="box visible">
    Tab 1 content
</div>
<div class="box">
    Tab 2 content
</div>
<div class="box">
    <div id="map" class="property-map" data-latitude="xxxxxx" data-longitude="xxxxxx"></div>
</div>
</div>

jQuery:

 $('ul.tabs').on('click', 'li:not(.current)', function() {
$(this).addClass('current').siblings().removeClass('current')
.parents('div.property').find('div.box').eq($(this).index()).fadeIn(150).siblings('div.box').hide();
})
$('ul.tabs').on('click', 'li:not(.current)', function() {
    google.maps.event.trigger(map, "resize");
})

CSS:

.property { margin-top:30px; }
.property .box { display: none; }
.property .box.visible { display: block; }
.property .tabs li { position:relative; float:left; margin:0 -1px 0 0;  padding:14px 35px; text-transform:uppercase; font-weight:600; color:#666; border:1px solid #e9e9e9; cursor:pointer; transition: color 0.3s ease-in-out; -webkit-transition: color 0.3s ease-in-out;}
.property .tabs li:hover { color:#000; }
.property .tabs li.current { margin-top:-2px; color:#000; border-top:3px solid #fcb042; border-bottom:1px solid #fff; background: #f0f0f0;}
.property .box { margin-top:-1px; padding:16px 19px 18px; line-height:20px; color:#666; border:1px solid #e9e9e9;}

.property-map { width:800px; height: 420px; }

主索引文件:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"" type="text/javascript"></script>
<script type="text/javascript">


function initialize_map() {

var latitude = $('#map').data('latitude');
var longitude = $('#map').data('longitude');

    var myLatlng = new google.maps.LatLng(latitude,longitude);
    var mapOptions = {
        zoom: 17,
        scrollwheel: true,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.SATELLITE
    };
    var map = new google.maps.Map(document.getElementById('map'), mapOptions);


    var marker = new google.maps.Marker({
        position: myLatlng,
        icon: '../images/new-marker.png',
        title:"Marker title",
        map: map

    });


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

</script>

【问题讨论】:

    标签: javascript jquery google-maps-api-3 tabs


    【解决方案1】:

    fade完成后,触发resize事件后需要设置地图的中心。

    $(this).addClass('current').siblings().removeClass('current')
      .parents('div.property').find('div.box').eq($(this).index()).fadeIn(150, 
        function() {
          google.maps.event.trigger(map, "resize"); 
          map.setCenter(mapOptions.center);
        })
      .siblings('div.box').hide();
    })
    

    proof of concept fiddle

    代码 sn-p:

    var mapOptions, map;
    
    function initialize_map() {
    
      var latitude = $('#map').data('latitude');
      var longitude = $('#map').data('longitude');
    
      var myLatlng = new google.maps.LatLng(latitude, longitude);
      mapOptions = {
        zoom: 17,
        scrollwheel: true,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.SATELLITE
      };
      map = new google.maps.Map(document.getElementById('map'), mapOptions);
    
    
      var marker = new google.maps.Marker({
        position: myLatlng,
        icon: 'http://maps.google.com/mapfiles/ms/micons/blue.png',
        title: "Marker title",
        map: map
    
      });
      $('ul.tabs').on('click', 'li:not(.current)', function() {
        $(this).addClass('current').siblings().removeClass('current')
          .parents('div.property').find('div.box').eq($(this).index()).fadeIn(150, function() {
            google.maps.event.trigger(map, "resize");
            map.setCenter(mapOptions.center);
          }).siblings('div.box').hide();
      })
    }
    google.maps.event.addDomListener(window, 'load', initialize_map);
    .property {
      margin-top: 30px;
    }
    .property .box {
      display: none;
    }
    .property .box.visible {
      display: block;
    }
    .property .tabs li {
      position: relative;
      float: left;
      margin: 0 -1px 0 0;
      padding: 14px 35px;
      text-transform: uppercase;
      font-weight: 600;
      color: #666;
      border: 1px solid #e9e9e9;
      cursor: pointer;
      transition: color 0.3s ease-in-out;
      -webkit-transition: color 0.3s ease-in-out;
    }
    .property .tabs li:hover {
      color: #000;
    }
    .property .tabs li.current {
      margin-top: -2px;
      color: #000;
      border-top: 3px solid #fcb042;
      border-bottom: 1px solid #fff;
      background: #f0f0f0;
    }
    .property .box {
      margin-top: -1px;
      padding: 16px 19px 18px;
      line-height: 20px;
      color: #666;
      border: 1px solid #e9e9e9;
    }
    .property-map {
      width: 800px;
      height: 420px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <div class="property">
      <ul class="tabs clearfix">
        <li class="current">Tab 1</li>
        <li>Tab 2</li>
        <li>Tab 3</li>
      </ul>
      <div class="box visible">Tab 1 content</div>
      <div class="box">Tab 2 content</div>
      <div class="box">
        <div id="map" class="property-map" data-latitude="42" data-longitude="-72"></div>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      • 2014-02-28
      • 1970-01-01
      • 2012-03-16
      • 1970-01-01
      相关资源
      最近更新 更多