この質問はすでに何度も解決されていますが、私の場合、すべての解決策で問題が解決するわけではありません。すでに多くの人が書いているように、コンテンツのさまざまな部分を表示するために使用されるタブ、つまりページロードで開くタブ1にマップをロードすると、正常に表示されます。
クリックして呼び出す必要がある他のタブのいずれかにマップをロードすると、想定どおりにマップが描画されません。
私はすでに google.maps.event.trigger(map, 'resize'); を追加しようとしました。タブがクリックされたときですが、何もしません。
これが私のコードです。私はこれに夢中になり、すべての提案を試すのに10時間以上費やしました!
これは、クリックされたタブを処理する部分です:
$(document).ready(function() {
//Default Action
$(".tab_content").hide(); //Hide all content
$("ul.tabs li:first").addClass("active").show(); //Activate first tab
$(".tab_content:first").show(); //Show first tab content
//On Click Event
$("ul.tabs li").click(function() {
$("ul.tabs li").removeClass("active"); //Remove any "active" class
$(this).addClass("active"); //Add "active" class to selected tab
$(".tab_content").hide(); //Hide all tab content
var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
if (activeTab=='#tab4') {
google.maps.event.trigger(map, 'resize');
map.setZoom( map.getZoom() );
}
$(activeTab).fadeIn(
); //Fade in the active content
return false;
});
});
アラートを使用して、コードが tab4 で呼び出されているかどうかを確認すると、機能します。
これがグーグルマップのコードです
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(<? echo $lattie; ?>, <? echo $langie; ?>),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var marker = new google.maps.Marker({
position: map.getCenter(),
map: map,
title: 'Click to zoom'
});
google.maps.event.addListener(map, 'center_changed', function() {
// 3 seconds after the center of the map has changed, pan back to the
// marker.
window.setTimeout(function() {
map.panTo(marker.getPosition());
}, 3000);
});
google.maps.event.addListener(marker, 'click', function() {
map.setZoom(8);
map.setCenter(marker.getPosition());
});
}
google.maps.event.addDomListener(window, 'load', initialize);
body タグで intitalize() を呼び出していますが、これをサイズ変更を呼び出している場所に変更しても解決しません。
最後に、 map-canvas で div を示すビット:
<li id="four">
<div id="map-canvas" style="width:575px; height:500px;float:left;border:1px solid #d74c15;"></div>
</li>
私が言ったように、私はこれに夢中になっています、私はそれを働かせることができません、どんな助けも大歓迎です!