-1
我已經爲我的谷歌地圖下面的代碼對象:谷歌地圖resize在不居中
geocoder.geocode({ 'address': googleAddress}, function (results, status){
if (status == google.maps.GeocoderStatus.OK) {
var mapOptions = { zoom: 16, center:results[0].geometry.location, mapTypeId:google.maps.MapTypeId.ROADMAP}
map = new google.maps.Map(element[0], mapOptions);
var marker1 = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
title: googleAddress
});
google.maps.event.addDomListener(window, "resize", function() {
google.maps.event.trigger(map, "resize");
map.setCenter(results[0].geometry.location);
});
}
})
此元素的「顯示:無」裏面開始的div,所以當我火了點擊事件,打開了DIV,我也火了窗口事件「調整大小」(加載整個地圖...你可以閱讀關於爲什麼要解僱調整大小別處這些問題):
showMap = function(){
if ($('#map').hasClass('hide')){
$('#map').removeClass('hide');
$window.dispatchEvent(new Event('resize'));
}
else{
$('#map').addClass('hide');
}
};
以此爲據可以作爲顯示在地圖上完整的div,問題是我的中心是錯誤的。我有一個標記位於傳入的地址(已驗證標記位於正確的位置),但標記離開了屏幕(剛剛離開左上角)。如果縮小一次點擊,標記就在左上角。
我也都試過(沒有運氣)
google.maps.event.addDomListener(window, "resize", function() {
var center = map.getCenter();
map.setCenter(center);
});
請提供證明此問題的[最小,完整,測試和可讀示例](http://stackoverflow.com/help/mcve)。 – geocodezip