Google 当非活动标签有 display:none 时,地图不在 jQuery 标签的中心
Google map not centered in jQuery tabs when inactive tab has display:none
我正在尝试为我的房地产网站实施 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>
淡入淡出完成后,需要在触发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();
})
代码片段:
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>
我正在尝试为我的房地产网站实施 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>
淡入淡出完成后,需要在触发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();
})
代码片段:
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>