Google Maps Multiple markers with the exact same location Not working

前端 未结 1 1538
一整个雨季
一整个雨季 2020-11-30 05:32

I want to check to see if any of the existing markers match the latlng of the new marker and if so then merge the info window/tooltip text.

This is what I tried to d

相关标签:
1条回答
  • 2020-11-30 06:02

    You need to :

    1. create the marker clusterer first.
    2. add the markers to the MarkerClusterer (and format your code so it is easier to read...).

      function createMarker(latlng,text) {
        var marker = new google.maps.Marker({
          position: latlng,
          map: map
        });
      
        ///get array of markers currently in cluster
        var allMarkers = mc.getMarkers();
      
        //check to see if any of the existing markers match the latlng of the new marker
        if (allMarkers.length != 0) {
          for (i=0; i < allMarkers.length; i++) {
            var existingMarker = allMarkers[i];
            var pos = existingMarker.getPosition();
      
            if (latlng.equals(pos)) {
              text = text + " & " + content[i];
            }                   
          }
        }
      
        google.maps.event.addListener(marker, 'click', function() {
          infowindow.close();
          infowindow.setContent(text);
          infowindow.open(map,marker);
        });
        mc.addMarker(marker);
        return marker;
      }
      

    working example

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