将标记添加到将进入标记群集的Google地图

时间:2017-09-17 06:11:22

标签: javascript google-maps google-maps-api-3 markerclusterer

我添加了4个位置,当页面加载并缩小时,会聚为4个组。

我添加了一个点击事件来向地图添加标记。我无法将这些添加的标记变成标记集群,就像我在脚本中添加的4个位置一样。

我该怎么做?

<div id="mapContainer">
  <div id="map"></div>
</div>

<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://www.gstatic.com/firebasejs/4.3.1/firebase.js"></script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCbdYz0sVFHyKAMVP051D_UI1PsbxQ92n8&callback=initMap"></script>
<script>
  function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 8,
      center: {lat: 33.034405, lng: -117.292928}
    });
    // Create an array of alphabetical characters used to label the markers.
    var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    // 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) {
      return new google.maps.Marker({
        position: location,
        label: labels[i % labels.length]
      });
    });

    function addMarker(location) {
      var marker = new google.maps.Marker({
        position: location,
        map: map
      });
      marker.push({event: latLng});
    }
    //Add Marker click function
    map.addListener('click', function(event) {
      addMarker(event.latLng);
      console.log(locations);
    });
    // Add a marker cluster to manage the markers.
    var markerCluster = new MarkerClusterer(map, markers, {
      imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
    });
  }
  var locations = [
    {lat: 33.034405, lng: -117.292300},
    {lat: 33.020933, lng: -117.285465},
    {lat: 33.047011, lng: -117.298916},
    {lat: 33.045600, lng: -117.298309},
  ];
</script>

1 个答案:

答案 0 :(得分:4)

修改您的事件监听器以将新创建​​的标记添加到标记集群:

function addMarker(location) {
  var marker = new google.maps.Marker({
    position: location,
    map: map
  });
  marker.push({event: latLng});
  //add this:
  markerCluster.addMarker(marker);
}

proof of concept fiddle

代码段

&#13;
&#13;
html,
body,
#mapContainer,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
&#13;
<div id="mapContainer">
  <div id="map"></div>
</div>

<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://www.gstatic.com/firebasejs/4.3.1/firebase.js"></script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script>
<script>
  function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 8,
      center: {
        lat: 33.034405,
        lng: -117.292928
      }
    });
    // Create an array of alphabetical characters used to label the markers.
    var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    // 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) {
      return new google.maps.Marker({
        position: location,
        label: labels[i % labels.length]
      });
    });

    function addMarker(location) {
      var marker = new google.maps.Marker({
        position: location,
        map: map
      });
      markerCluster.addMarker(marker);
    }
    //Add Marker click function
    map.addListener('click', function(event) {
      addMarker(event.latLng);
      console.log(locations);
    });
    // Add a marker cluster to manage the markers.
    var markerCluster = new MarkerClusterer(map, markers, {
      imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
    });
  }
  var locations = [{
      lat: 33.034405,
      lng: -117.292300
    },
    {
      lat: 33.020933,
      lng: -117.285465
    },
    {
      lat: 33.047011,
      lng: -117.298916
    },
    {
      lat: 33.045600,
      lng: -117.298309
    },
  ];
</script>
&#13;
&#13;
&#13;