簡體   English   中英

將標記添加到Google地圖中,該標記將進入標記群集

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

我添加了4個位置,當頁面加載並縮小時,這些位置會聚集成4個位置。

我添加了一個click事件,以將標記添加到地圖。 我無法使這些添加的標記成為像我在腳本中放置的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>

修改事件偵聽器,以將新創建​​的標記也添加到標記集群:

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

概念證明

代碼段:

 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