【问题标题】:Google map API v3 - Add multiple infowindowsGoogle map API v3 - 添加多个信息窗口
【发布时间】:2015-05-03 10:47:48
【问题描述】:

我有一个工作部分的 google maps javascript,我确实遇到了问题。

现在我遇到的问题是只显示了一个信息窗口,最后一个。我在另一个有效的堆栈线程上找到了解决方案。但我真的不知道为什么。我对 Javascript 还很陌生,所以我希望有人可以向我解释发生了什么变化以及如何变化。

这是工作代码:

function setMarkers(map, locations){
  for(var i = 0; i < locations.length; i++){
    var marker = locations[i];
    var latLng = new google.maps.LatLng(locations[i][1], locations[i][2]);
    var content = locations[i][0];
    var infowindow = new google.maps.InfoWindow();

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

    google.maps.event.addListener(marker, 'click', function(content){
      return function(){
        infowindow.setContent(content);
        infowindow.open(map, this);
      }
    }(content));
  }
}

这是原始的损坏代码(我将仅粘贴更改的代码):

 google.maps.event.addListener(marker, 'click', function(){
      infowindow.setContent(content);
      infowindow.open(map, marker);
    });

现在我想知道的是:

  1. return fn 的作用是什么,以及

  2. addListener (}(content));) 末尾添加的(content) 有什么作用,因为据我所知,内容已在setContent 属性中设置。

    李>

谢谢!

【问题讨论】:

  • 感谢您的编辑,让您的眼睛更轻松。堆栈格式对我来说还是有点新鲜。
  • 你可能想改变你的问题的标题,它看起来太笼统了。

标签: javascript google-maps-api-3


【解决方案1】:

不要循环您的infowindow...
您只需要infowindow 对象一个实例
将它移到循环之外,与您的函数相同。
在循环内部,将其内容作为 relative 分配给点击的marker

const locations = [
  {lat: 45.840196, lng: 15.964331, name: "Zagreb"},
  {lat: 43.514851, lng: 16.449083, name: "Split"},
  {lat: 42.645725, lng: 18.094058, name: "Dubrovnik"},
];

function initGoogleMap(){

  const infowindow = new google.maps.InfoWindow(); // Only one InfoWindow
  const map = new google.maps.Map(document.getElementById('map-canvas'), {
      zoom: 6,
      center: new google.maps.LatLng(45, 15)
  });
  
  function placeMarker( loc ) {
    const marker = new google.maps.Marker({
      position : new google.maps.LatLng( loc.lat, loc.lng ),
      map : map
    });
    google.maps.event.addListener(marker, 'click', function(){
        infowindow.close(); // Close previously opened infowindow
        infowindow.setContent(`<div id="infowindow">${loc.name}</div>`);
        infowindow.open(map, marker);
    });
  }
  
  // ITERATE ALL LOCATIONS. Pass every location to placeMarker
  locations.forEach( placeMarker );
  
}

google.maps.event.addDomListener(window, 'load', initGoogleMap);
html, body, #map-canvas { height: 100%; margin: 0px; }
#infowindow{ padding: 10px; }
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<div id="map-canvas"></div>

【讨论】:

  • 对我不起作用,但确实如此。我将位置添加到标记对象“marker.loc = loc;”然后在 addListener 函数中以“this.loc[0]”的形式访问它。 addListener func 范围是标记对象。
  • 工作就像一个魅力。感谢您为我节省了一些时间。
【解决方案2】:

出于某种原因,我不得不修改标记对象并在事件监听器函数中访问该数据。

marker上点击事件函数的作用域是marker对象。

var infowindow = new google.maps.InfoWindow();

for(int i = 0; i < locations.length; i++){
    var latLng = new google.maps.LatLng(locations[i][1], locations[i][2]);
    var contentString = locations[i][0];

    marker = new google.maps.Marker({           
          position: latLng,
          map: map,
          contentString: contentString
    });
    marker.data = locations[i]; // adds object to marker object


    marker.addListener('click', function() {
        // read custom data in this.data   
        infowindow.setContent("<div id='infowindow'>"+ this.data[0] +"</div>");

        infowindow.open(map, this);
        map.setCenter(this.getPosition());
     });
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-02-26
  • 1970-01-01
  • 2012-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多