【问题标题】:Multiple Marker InfoWindow in Google Maps v3Google Maps v3 中的多个标记信息窗口
【发布时间】:2011-06-22 19:24:58
【问题描述】:

我已经对此进行了大约 1 小时的测试。但由于某种原因,我无法让它工作,为每个标记显示另一个信息窗口文本。 单击它们中的每一个,将打开一个新的信息窗口(旧的将关闭这很好),但窗口内的文本始终是我在最后一个标记中设置的文本:

function initialize() {
    var center = new google.maps.LatLng(51.133333,10.416667);
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 6,
      center: center,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    var markers = [];

    for (var i = 0, dataPhoto; dataPhoto = data.photos[i]; i++) {
      var latLng = new google.maps.LatLng(dataPhoto.latitude,
                   dataPhoto.longitude);
      var marker = new google.maps.Marker({
        position: latLng,
        title: dataPhoto.infotitle,
        map: map,
      });

      /* Create Info Windows */         

      var infowindow = new google.maps.InfoWindow({
        content: " "
      });
      google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent('<h3>'+marker.title+'</h3>'+' Infotext');
        infowindow.open(map, this);
      });
      markers.push(marker);
    }
    var markerCluster = new MarkerClusterer(map, markers);
} 

我在这里看到了一些解决方案,但它们似乎都不适合我。 非常感谢您的帮助!

online demo

【问题讨论】:

    标签: google-maps-api-3 google-maps-markers infowindow


    【解决方案1】:

    您只需将 Content 设置为 this.title,而不是 marker.title

    infowindow.setContent('<h3>'+this.title+'</h3>'+' Infotext');
    

    谢谢 dz46

    【讨论】:

      【解决方案2】:

      你没有任何方法来存储 infoWindow.. 看看我的代码,我使用 infoBubble 库,但它应该非常相似。这可能比您需要的代码多,但越多越好..

      /**
       * infoBubble Variable
       * This variable is globally defined for defaults that are loaded.
       */
      var infoBubbles = [];
      /**
       * array of all of the markers that are on the map
       * 
       * @type {Array}
       * @author Mike DeVita
       * @copyright 2011 MapIT USA
       * @category map_Js
       */
      var markersArray = [];
      /**
       * array of all of the sidebar items for all of the markers on the map
       * 
       * @type {Array}
       * @author Mike DeVita
       * @copyright 2011 MapIT USA
       * @category map_Js
       */
      var sidebarHtmlArray = [];
      
      /**
       * setPoints(locations)
       * 
       * sets the marker, infoBubble and sidebarItem based on the locations 
       * that were returned from the JSON query.
       * 
       * @param {array} locations array of all of the points, and their settings/html
       * 
       * @author Mike DeVita
       * @author Google Maps API
       * @copyright 2011 MapIT USA
       * @category map_js
       */     
      function setPoints(locations){      
          for (var i = 0; i < locations.length; i++) {
              /** @type {array} reassigns locations array to be point, isolates it to the setPoints() function */
              var point = locations;
              /** @type {google} generates Googles API form of the lat lng passed from var point */
              var myLatLng = new google.maps.LatLng(point[i].lat, point[i].lng);
      
              /**
               * marker variable, stores all of the information pertaining to a specific marker
               * this variable creates the marker, places it on the map and then also sets some
               * custom options for the infoBubbles.
               * 
               * @type {google}
               */
              var marker = new google.maps.Marker({
                  position: myLatLng,
                  map: map,
                  animation: google.maps.Animation.DROP,
                  icon: point[i].marker_icon
              });
      
              /** push the marker to the markersArray to delete or show the overlays */
              markersArray.push(marker);
      
              var sidebarItem = point[i].sidebarHtmlView;
              sidebarHtmlArray.push(sidebarItem);
              infoBubbles[i] = new InfoBubble({ 
                  map: map, 
                  minHeight: point[i].min_height,
                  maxHeight: point[i].max_height,
                  minWidth: point[i].min_width,
                  maxWidth: point[i].max_width,
                  disableAutoPan: false, 
                  hideCloseButton: false, 
                  arrowPosition: 30, 
                  padding:12
              }); 
              var tabs = point[i].tabs;
              infoBubbles[i].addTab('Company', point[i].html);
              for (var ii = 0; ii < tabs.length; ii++) {
                  infoBubbles[i].addTab(tabs[ii].tabTitle, tabs[ii].tabContent);
              }
              /**
               * add the listeners for the markerClicks and the sideBarClicks 
               * 
               * @type {google}
               * @todo eventDomListener does not work yet, this is the click listener of the sidebar item's
               */
              google.maps.event.addListener(marker, 'click', handleMarkerClick(marker, i)); 
          }   
      }
      
      
      function handleMarkerClick(marker,index) { 
          return function() { 
              if (!infoBubbles[index].isOpen()) { 
                  infoBubbles[index].open(map, marker); 
              }else{
                  infoBubbles[index].close(map, marker); 
              }
          } 
      }
      
      /**
       * addmarker(location)
       * 
       * adds the marker to the map and pushes the marker
       * to the end of the markersArray
       * 
       * @param {google} location LatLng of where the marker should be put
       * 
       * @author Mike DeVita
       * @author Google API
       * @copyright 2011 MapIT USA
       * @category map_js
       */
      function addMarker(location, marker_icon){
          marker = new google.maps.Marker({
              position: location,
              map: map,
              animation: google.maps.Animation.DROP,
              icon: marker_icon
          });
          markersArray.push(marker);
      }
      

      【讨论】:

        【解决方案3】:

        将您的代码更改为

        var markers = new Array();
        var infowindow = new Array();
        var map;
        function initialize() {
            var center = new google.maps.LatLng(51.133333,10.416667);
            map = new google.maps.Map(document.getElementById('map'), {
              zoom: 6,
              center: center,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            });
        
            for (var i = 0, dataPhoto; dataPhoto = data.photos[i]; i++) {
              var latLng = new google.maps.LatLng(dataPhoto.latitude,
                           dataPhoto.longitude);
             markers[i] = new google.maps.Marker({
                position: latLng,
                title: dataPhoto.infotitle,
                map: map,
              });
              /* Create Info Windows */         
        
              infowindow[i] = new google.maps.InfoWindow({
                content: " "
              });
              google.maps.event.addListener(markers[i], 'click', function() {
                infowindow[i].setContent('<h3>'+this.title+'</h3>'+' Infotext');
                infowindow[i].open(map, this);
              });
        
            }
            var markerCluster = new MarkerClusterer(map, markers);
        } 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-05-14
          • 2011-02-02
          • 1970-01-01
          • 2012-06-06
          • 2018-04-02
          • 2015-11-21
          • 2013-08-14
          • 1970-01-01
          相关资源
          最近更新 更多