2012-04-26 85 views
21

我正在使用Google Maps api-v3, 的項目工作在地圖上會有幾個地點標記,其中包含我存儲在信息窗口。Google maps-api v3 InfoWindow在頁面加載時自動打開

我想知道有無論如何,你可以設置InfoWindow自動打開頁面加載(即自動打開,無需用戶交互)。

在線搜索我可以發現的所有東西都需要綁定到事件監聽器,但InfoWindow對象似乎具有的所有事件都是鼠標事件。

有沒有人知道各種解決方法?

回答

26

不知道我完全理解你的問題,但這個工作對我來說有一個硬編碼的經緯度:

var infoWindow = null; 
function initialize() 
{ 
    infoWindow = new google.maps.InfoWindow(); 
    var windowLatLng = new google.maps.LatLng(43.25,-68.03); 
    infoWindow.setOptions({ 
     content: "<div>This is the html content.</div>", 
     position: windowLatLng, 
    }); 
    infoWindow.open(map); 
} // end initialize 

google.setOnLoadCallback(initialize); 
+0

太感謝你了,這正是我一直在尋找! – scrineym 2012-04-27 01:50:21

+0

信息窗口正好落在我的標記上。有沒有一種方法可以計算經度差異,以便將信息窗口置於標記之上? – user3411192 2016-09-22 18:01:04

11

我非常遲到了這個問題,但我想分享something.I還關注其解決方案甚至沒有得到上述解決方案的工作。然後我嘗試了自己並得到了自己的解決方案(我不是專家,我的想法可能不好,但對我來說工作很好)。 我們一般不喜歡

var infowindow = new google.maps.InfoWindow(); 
google.maps.event.addListener(marker, 'click', (function(marker) { 
return function() { 
    infowindow.setContent('some content here'); 
     infowindow.open(map, marker); 
    } 
})(marker)); 

與替換上述行:

var infowindow = new google.maps.InfoWindow(); 
infowindow.setContent('some content here'); 
infowindow.open(map, marker); 

不要等待標記的單擊事件只是設置的內容並打開它(你必須定義地圖,標記和信息窗口指向你的標記)。

+0

@Roka ..謝謝..這是我的作品:) – AnNaMaLaI 2013-03-05 08:58:57

+0

最簡單的解決方案,稱之爲外部標記點擊..謝謝羅卡 – 2015-12-13 07:42:09

3

只需要infowindow.open(地圖,標記);退出google.maps.event.addListener。它應該看起來像這樣:

 var marker = new google.maps.Marker({ 
     position: myLatlng1, 
     map: map, 
     title: 'Uluru (Ayers Rock)' 

    }); 

    infowindow.open(map,marker); 

    google.maps.event.addListener(marker, 'click', function() { 
    }); 
1

這是我的代碼,加載信息窗口與地圖和鼠標懸停。

map.setCenter(results[0].geometry.location); 
var marker = new google.maps.Marker({ 
    map: map, 
    icon: "/images/map-marker.png", 
    draggable: true, 
    position: results[0].geometry.location 

}); 
var infowindow = new google.maps.InfoWindow({ 
    content: "Use pin to point your exact locality" 
}); 
google.maps.event.addListener(marker, 'mouseover', function() { 
    infowindow.open(map, marker); 
}); 
infowindow.open(map, marker); 
0

function initMap() { 
 
    var cairo = {lat:18.519331, lng: 73.849421}; 
 
    var map = new google.maps.Map(document.getElementById('map'), { 
 
     scaleControl: true, 
 
     center: cairo, 
 
     zoom: 15, 
 
    }); 
 

 
    var infowindow = new google.maps.InfoWindow; 
 
    infowindow.setContent('<b>adddress</b>'); 
 

 
    var marker = new google.maps.Marker({map: map, position: cairo}); 
 
    infowindow.open(map, marker); 
 
    infoWindow.open(map); 
 
}
<script async defer 
 
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCrCByzXY6y5KMSOi9AuqWVf4VYZPyJ5SE&language=hi&region=EG&callback=initMap"> 
 
</script> 
 
<div id="map"></div>

+0

這將是更好的,如果你解釋爲什麼你的代碼將工作。 – 2017-09-16 07:05:53

相關問題