谷歌地图api群集标记和自定义信息和图标

时间:2018-02-04 14:23:04

标签: javascript api google-maps maps

我遇到自定义图标问题。我已设法在标记上获得不同的信息文本,我已设法让集群工作,但当我添加var icon1和var icon2并将它们放在位置数组中:“icon:icon2。全部失败,有没有办法同时使用icon,infowindow和clustermarkers?

<!DOCTYPE html>
    <html>
      <head>
       <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Marker Clustering</title>
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>

      function initMap() {




        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 10,
          center: {lat: 63.418719, lng: 10.3685516}

        });


         var icon1 = {
        url: "https://maps.google.com/mapfiles/kml/shapes/parking_lot_maps.png", // url
        scaledSize: new google.maps.Size(50, 50), // size
        };

         var icon2 = {
        url: "https://maps.google.com/mapfiles/kml/shapes/library_maps.png", // url
        scaledSize: new google.maps.Size(50, 50), // size
        };


        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;
        });

        var markerCluster = new MarkerClusterer(map, markers,
            {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
      }
      var locations = [
        {lat: 63.131623,  lng: 10.370243, info:"Test1", icon: icon1},
        {lat: 62.432600,  lng: 10.300243, info:"Test2", icon: icon2},
        {lat: 62.330642,  lng: 10.300243, info:"Test2", icon: icon1},
        {lat: 63.530691,  lng: 10.300243, info:"Test2", icon: icon2},



      ]
    </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=AIzoVQ&callback=initMap">
    </script>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

要实际使用位置数组中的图标,请更改标记定义:

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;
});

要:

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

proof of concept fiddle

enter image description here

代码段

&#13;
&#13;
function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 8,
    center: {
      lat: 63.418719,
      lng: 10.3685516
    }

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

  var markerCluster = new MarkerClusterer(map, markers, {
    imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
  });
}

var icon1 = {
  url: "https://maps.google.com/mapfiles/kml/shapes/parking_lot_maps.png", // url
  scaledSize: new google.maps.Size(50, 50), // size
};

var icon2 = {
  url: "https://maps.google.com/mapfiles/kml/shapes/library_maps.png", // url
  scaledSize: new google.maps.Size(50, 50), // size
};

var locations = [{
  lat: 63.131623,
  lng: 10.370243,
  info: "Test1",
  icon: icon1
}, {
  lat: 62.432600,
  lng: 10.300243,
  info: "Test2",
  icon: icon2
}, {
  lat: 62.330642,
  lng: 10.300243,
  info: "Test2",
  icon: icon1
}, {
  lat: 63.530691,
  lng: 10.300243,
  info: "Test2",
  icon: icon2
}, ]
google.maps.event.addDomListener(window, "load", initMap);
&#13;
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places&ext=.js"></script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>
<div id="map"></div>
&#13;
&#13;
&#13;