2010-07-23 49 views
3

我有一個google.maps.InfoWindow實例,它有一個自定義關閉按鈕。這是由onClick事件觸發的。然而,如果我點擊關閉按鈕,信息窗口就會關閉,這是預期的,但是在地圖上出現一個新的標記,在信息窗口使用的地方。它看起來像onClick =「infoWindow.close()」是placeReclameMarker(event.latLng);同時。信息窗口自定義關閉按鈕

var map; 
    var infoWindow; 
    function initialize() { 
     var latlng = new google.maps.LatLng(47.030698, 28.850098); 
     var myOptions = { 
      zoom: 15, 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     map = new google.maps.Map(
      document.getElementById("map_canvas"), myOptions); 

     google.maps.event.addListener(map, 'click', function (event) { 
      placeReclameMarker(event.latLng); 
     }); 

    } 

    function placeReclameMarker(location) { 

     var marker = new google.maps.Marker({ 
      position: location, 
      draggable: true, 
      map: map 
     }); 
     google.maps.event.addListener(marker, 'rightclick', function() { 
      infoWindow = new google.maps.InfoWindow({ 
       content: '<input type="button" onclick="infoWindow.close()">' 
      }); 
      infoWindow.open(map, marker); 
     }); 
    } 
+0

它看起來像它是一個谷歌地圖的bug /問題。問題在新版本中消失。 – alagar 2016-06-01 15:30:44

回答

1

我不確定您可以訪問infoWindow對象內部的infoWindow變量。 嘗試:

content: '<input type="button" onclick="parent.infoWindow.close()">' 

content: '<input type="button" onclick="parent.parent.infoWindow.close()">' 

content: '<input type="button" onclick="alert(infoWindow)">' 

信息窗口變量不能是未定義的。

好運

0

我遇到了同樣的問題。找到解決方案。關鍵是你不僅要關閉InfowWindow,還要殺死標記。

試試這個;

var map; 
var marker; 
var infoWindow; 
function whateverFunction() { 
    // some code here to create the marker and the infoWindow 
    infoWindow.open(map, marker); 
    infoWindow.setContent('<input type=button name=Close onClick="marker.setMap(null);">'); 
} 

完全適合我。