Integrate Google Maps MarkerClusterer with infowindow

后端 未结 1 1653
执念已碎
执念已碎 2020-12-16 04:21

I\'m trying to put InfoWindow in multiple markers grouped with MarkerClusterer, but without sucess. I only can generate maps with infowindows OR with cluster; not both at sa

1条回答
  •  离开以前
    2020-12-16 05:08

    Add the "unique information" for each marker to your locations array (like the answer to this question: Google Maps JS API v3 - Simple Multiple Marker Example).

    Add a click listener to each marker which opens the InfoWindow with that unique content.

    var infoWin = new google.maps.InfoWindow();
    var markers = locations.map(function(location, i) {
      var marker = new google.maps.Marker({
        position: location
      });
      google.maps.event.addListener(marker, 'click', function(evt) {
        infoWin.setContent(location.info);
        infoWin.open(map, marker);
      })
      return marker;
    });
    

    proof of concept fiddle

    code snippet:

    function initMap() {
    
      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 5,
        center: {
          lat: -15.7942357,
          lng: -47.8821945
        }
      });
      var infoWin = new google.maps.InfoWindow();
      // Add some markers to the map.
      // Note: The code uses the JavaScript Array.prototype.map() method to
      // create an array of markers based on a given "locations" array.
      // The map() method here has nothing to do with the Google Maps API.
      var markers = locations.map(function(location, i) {
        var marker = new google.maps.Marker({
          position: location
        });
        google.maps.event.addListener(marker, 'click', function(evt) {
          infoWin.setContent(location.info);
          infoWin.open(map, marker);
        })
        return marker;
      });
    
      // Add a marker clusterer to manage the markers.
      var markerCluster = new MarkerClusterer(map, markers, {
        imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
      });
    
    }
    var locations = [{
      lat: -19.9286,
      lng: -43.93888,
      info: "marker 1"
    }, {
      lat: -19.85758,
      lng: -43.9668,
      info: "marker 2"
    }, {
      lat: -18.24587,
      lng: -43.59613,
      info: "marker 3"
    }, {
      lat: -20.46427,
      lng: -45.42629,
      info: "marker 4"
    }, {
      lat: -20.37817,
      lng: -43.41641,
      info: "marker 5"
    }, {
      lat: -20.09749,
      lng: -43.48831,
      info: "marker 6"
    }, {
      lat: -21.13594,
      lng: -44.26132,
      info: "marker 7"
    }, ];
    
    google.maps.event.addDomListener(window, "load", initMap);
    html,
    body,
    #map {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    
    
    

    0 讨论(0)
提交回复
热议问题