简体   繁体   English

将标记添加到Google地图中,该标记将进入标记群集

[英]Add Markers to Google Maps that will go into a Marker Cluster

I have added 4 locations that, when the page loads and you zoom out, will cluster into a group of 4. 我添加了4个位置,当页面加载并缩小时,这些位置会聚集成4个位置。

I have added a click event to add markers to the map. 我添加了一个click事件,以将标记添加到地图。 I cannot get those added markers to become a marker cluster like the 4 locations I have put into my script. 我无法使这些添加的标记成为像我在脚本中放置的4个位置一样成为标记集群的标记。

How do I do that? 我怎么做?

<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>

Modify your event listener to also add the newly created marker to the marker cluster: 修改事件侦听器,以将新创建​​的标记也添加到标记集群:

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 概念证明

code snippet: 代码段:

 html, body, #mapContainer, #map { height: 100%; width: 100%; margin: 0px; padding: 0px } 
 <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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM