且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Google Map API v3〜只需关闭一个infowindow?

更新时间:2022-12-27 14:27:55

使用 v3 API ,您可以轻松关闭 InfoWindow a href =http://code.google.com/apis/maps/documentation/javascript/reference.html#InfoWindow =noreferrer> InfoWindow.close() 方法。您只需保留对您正在使用的 InfoWindow 对象的引用。考虑下面的例子,它打开一个 InfoWindow 并在5秒后关闭它:

 <!DOCTYPE html> 
< html>
< head>
< meta http-equiv =content-typecontent =text / html; charset = UTF-8/>
< title> Google Maps API InfoWindow Demo< / title>
< script src =http://maps.google.com/maps/api/js?sensor=false
type =text / javascript>< / script>
< / head>
< body>
< div id =mapstyle =width:400px; height:500px;>< / div>

< script type =text / javascript>
var map = new google.maps.Map(document.getElementById('map'),{
zoom:4,
center:new google.maps.LatLng(-25.36388,131.04492) ,
mapTypeId:google.maps.MapTypeId.ROADMAP
});

var marker = new google.maps.Marker({
position:map.getCenter(),
map:map
});

var infowindow = new google.maps.InfoWindow({
content:'InfoWindow'
});

infowindow.open(map,marker);

setTimeout(function(){infowindow.close();},5000);
< / script>
< / body>
< / html>

如果您有单独的 InfoWindow 对象每个 Marker ,您可能需要考虑添加 InfoWindow 对象作为标记的属性 objects:

  var marker = new google.maps.Marker({
position: map.getCenter(),
map:map
});

marker.infowindow = new google.maps.InfoWindow({
content:'InfoWindow'
});

然后你就可以打开和关闭 InfoWindow $>,如下所示:

  marker.infowindow.open(map,marker); 
marker.infowindow.close();

如果您有一组标记,则同样适用:

  var markers = []; 

marker [0] = new google.maps.Marker({
position:map.getCenter(),
map:map
});

marker [0] .infowindow = new google.maps.InfoWindow({
content:'InfoWindow'
});

// ...

marker [0] .infowindow.open(map,marker);
marker [0] .infowindow.close();


Trying to simply close an infowindow?

I already have an array of markers, so something like this would be good. Thanks

MyMarkers[i].infowindow.close();

With the v3 API, you can easily close the InfoWindow with the InfoWindow.close() method. You simply need to keep a reference to the InfoWindow object that you are using. Consider the following example, which opens up an InfoWindow and closes it after 5 seconds:

<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
  <title>Google Maps API InfoWindow Demo</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>
</head> 
<body>
  <div id="map" style="width: 400px; height: 500px;"></div>

  <script type="text/javascript">
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 4,
      center: new google.maps.LatLng(-25.36388, 131.04492),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var marker = new google.maps.Marker({
      position: map.getCenter(),
      map: map
    });

    var infowindow = new google.maps.InfoWindow({
      content: 'An InfoWindow'
    });

    infowindow.open(map, marker);

    setTimeout(function () { infowindow.close(); }, 5000);
  </script>
</body>
</html>

If you have a separate InfoWindow object for each Marker, you may want to consider adding the InfoWindow object as a property of your Marker objects:

var marker = new google.maps.Marker({
  position: map.getCenter(),
   map: map
});

marker.infowindow = new google.maps.InfoWindow({
  content: 'An InfoWindow'
});

Then you would be able to open and close that InfoWindow as follows:

marker.infowindow.open(map, marker);
marker.infowindow.close();

The same applies if you have an array of markers:

var markers = [];

marker[0] = new google.maps.Marker({
  position: map.getCenter(),
   map: map
});

marker[0].infowindow = new google.maps.InfoWindow({
  content: 'An InfoWindow'
});

// ...

marker[0].infowindow.open(map, marker);
marker[0].infowindow.close();