2010-07-23 50 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);">'); 
} 

完全适合我。