Google Map API v3 ~ 只需关闭一个信息窗口? [英] Google Map API v3 ~ Simply Close an infowindow?

查看:33
本文介绍了Google Map API v3 ~ 只需关闭一个信息窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图简单地关闭信息窗口?

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();

推荐答案

使用 v3API,您可以使用 InfoWindowrel="noreferrer">InfoWindow.close() 方法.您只需要保留对您正在使用的 InfoWindow 对象的引用.考虑以下示例,它打开一个 InfoWindow 并在 5 秒后关闭它:

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>

如果您为每个 Marker 设置了一个单独的 InfoWindow 对象,您可能需要考虑将 InfoWindow 对象添加为您的 InfoWindow 对象code>Marker 对象:

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'
});

然后您就可以按如下方式打开和关闭该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();

这篇关于Google Map API v3 ~ 只需关闭一个信息窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆