JavaScript 谷歌地图 API - 一次打开一个信息窗口..

发布于 2024-09-15 06:35:25 字数 579 浏览 5 评论 0原文

这个问题与谷歌地图 API v3 中的信息窗口相关。

目前我循环这个函数并放置标记。

function addPostCode(zip, html) 
{
   geocoder.geocode( { 'address': zip}, function(results, status) 
   {
      if (status == google.maps.GeocoderStatus.OK)
      {
         map.setCenter(results[0].geometry.location);
         var marker = new google.maps.Marker({
         map: map,
         position: results[0].geometry.location,
         name: zip
      });
});

现在我想添加一个具有独特 HTML 的信息窗口,但是我想要以下行为。

打开信息窗口时通过事件,任何当前的信息窗口都将关闭,只留下新的信息窗口。

这可能吗?我将如何处理?事实证明,查找有关此问题的文档很困难。

This questions relating to infowindow in the google maps API v3..

Currently I loop this function and place markers..

function addPostCode(zip, html) 
{
   geocoder.geocode( { 'address': zip}, function(results, status) 
   {
      if (status == google.maps.GeocoderStatus.OK)
      {
         map.setCenter(results[0].geometry.location);
         var marker = new google.maps.Marker({
         map: map,
         position: results[0].geometry.location,
         name: zip
      });
});

Now I would like to add an info windows with unique HTML, however I would like the following behaviour..

When an infowindow is opened by an event, any current infowindows will close leaving only the new one present..

Is this possible and how would I go about it? Finding documentation on this issue is proving difficult..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

醉梦枕江山 2024-09-22 06:35:25

在初始化中创建一个信息窗口。在您想要打开信息窗口的事件/侦听器中,您可以设置内容并在地图上的标记/位置上打开信息窗口。

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

function add_marker(point, name, content)
{
   var marker = new google.maps.Marker({
      map: map,
      position: point,
      dragable: false,
      clickable: true,
      name: name
   });
   marker.content = content;
   google.maps.event.addListener(marker, 'click', function()
   {
      infowindow.content = marker.content;
      infowindow.open(map, marker);
   });
   return marker;
};

function addPostCode(zip, html) 
{
   geocoder.geocode( { 'address': zip}, function(results, status) 
   {
      if (status == google.maps.GeocoderStatus.OK)
      {
         map.setCenter(results[0].geometry.location);
         var marker = add_marker(results[0].geometry.location, zip, html)
      });
});

这个问题和答案帮助我解决了单个或多个信息窗口问题:

Google Maps API v3 向每个标记添加信息窗口

Create a single infowindow in your initialization. In your event/listener where you want to open the infowindow, you'd set the content and open the infowindow on the marker/location on the map.

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

function add_marker(point, name, content)
{
   var marker = new google.maps.Marker({
      map: map,
      position: point,
      dragable: false,
      clickable: true,
      name: name
   });
   marker.content = content;
   google.maps.event.addListener(marker, 'click', function()
   {
      infowindow.content = marker.content;
      infowindow.open(map, marker);
   });
   return marker;
};

function addPostCode(zip, html) 
{
   geocoder.geocode( { 'address': zip}, function(results, status) 
   {
      if (status == google.maps.GeocoderStatus.OK)
      {
         map.setCenter(results[0].geometry.location);
         var marker = add_marker(results[0].geometry.location, zip, html)
      });
});

This question and answer helped me out quite a bit with the single or multiple Info Window issue:

Google Maps API v3 adding an InfoWindow to each marker

半山落雨半山空 2024-09-22 06:35:25
var currentInfoWindow = ''; //Global variable

google.maps.event.addListener(marker, 'click', function()
   {
      if(currentInfoWindow != '')
      { 
        currentInfoWindow.close();
        currentInfoWindow = '';
      }
      var infoWindow = new google.maps.InfoWindow({content: 'COntent'});
      infowindow.open(map, marker);
      currentInfoWindow = infowindow;
   });
var currentInfoWindow = ''; //Global variable

google.maps.event.addListener(marker, 'click', function()
   {
      if(currentInfoWindow != '')
      { 
        currentInfoWindow.close();
        currentInfoWindow = '';
      }
      var infoWindow = new google.maps.InfoWindow({content: 'COntent'});
      infowindow.open(map, marker);
      currentInfoWindow = infowindow;
   });
我也只是我 2024-09-22 06:35:25

您最好向 Google 网上论坛询问 Google Maps API v3 关于此问题以及任何其他相关问题。

我没有使用过 API v3,但据我所知,一次只能打开一个信息窗口,所以这会自动发生。

You are probably better off asking the Google Groups for the Google Maps API v3 about this one, and any other related questions.

I haven't used API v3, but as far as I know, you can only have one info window open at a time, so this would happen automatically.

故笙诉离歌 2024-09-22 06:35:25

有两种方法可以做到这一点...第一种是创建一个信息窗口并仅使用 .setOptions 信息窗口的方法,用于更新 内容

第二种方法是简单地在代码中设置一个模块级变量来包含“活动”窗口...然后如果有任何 infoWindow 打开,请在您之前调用它的 .close 方法打开新的。

There are two ways to do this ... the first is to create a single info window and just use the .setOptions method of the info window to update the content.

The second way to do it is to simply set a module-level variable in your code to contain the "active" window ... and then if any infoWindow is open, call it's .close method before you open the new one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文