2011-12-01 79 views
1

我想將InfoWindow放置在我的谷歌地圖中心的地圖窗口中。這可能嗎?谷歌地圖InfoWindow放置在地圖的中心嗎?

不幸的是存在這樣的

var infowindow = new google.maps.InfoWindow({ 
    positionMiddleOfWindow: true }); 

有人知道如何將信息窗口地圖窗口中間的信息窗口選項別無選擇?

回答

3

你可以得到你的地圖的中心座標,然後創建與該位置

var lat = map.getCenter().lat(); 
var lng = map.getCenter().lng(); 

信息窗口或在一塊得到的經緯度: var latlng = map.getCenter();

+0

MHM ...我嘗試這樣做,添加到我的信息窗口下面的信息窗口VAR =新google.maps.InfoWindow({內容:contentString,位置:經緯度(緯度,經度)});但現在有一個錯誤 – blackrafi

+0

我已經指出一個Lat Lng的地址,這個標記有一個信息框,我希望信息框位於地圖的中間而不是標記的位置。 – blackrafi

+0

這個怎麼樣:'position:map.getCenter()'? – Rob

3

首先,地圖設置以標記爲中心。您可以檢查InfoWindow的domready事件並左右滾動地圖,使InfoWindow居中。

的jQuery

pin=new google.maps.LatLng(50.00, 50.00); 

marker=new google.maps.Marker({ 
    position:pin, 
}); 

marker.setMap(map); 
map.setCenter(marker.getPosition()); 

// The InfoWindow 
myWindow = new google.maps.InfoWindow({ 
    content:'<p id="myId">My InfoWindow</p>', 
}); 

myWindow.open(map, marker); 

google.maps.event.addListener(myWindow, 'domready', function() { 
    // Get the actual height of the InfoWindow 
    e = $('#myId').parent().parent().parent(); 
    h = parseFloat(e.height()); 
    w = parseFloat(e.width()); 

    // Move the map a little to the left and down 
    map.panBy(-w/2, h/2) 
}); 
1

相反移動地圖或計算的高度或寬度只是使用下面的函數的。這裏的訣竅是在我們打開infoWindow之前延遲一段時間。我們應該setZoom和setCenter和延遲300毫秒我們打開信息窗口和谷歌地圖API將在市中心打開信息窗口應該如何前:

function OpenInfoWindowCentralized(map, infowindow, marker) { 
    map.setZoom(16); 
    map.setCenter(marker.getPosition()); 

    setTimeout(function() { 
     infowindow.open(map, marker); 
    }, 300); 
} 

我希望這有助於以某種方式...

乾杯

+1

這是問題的正確解決方案 –