Javascript GoogleMapAPI:仅将MarkerClusterer应用于特定标记的类别

Javascript GoogleMapAPI:仅将MarkerClusterer应用于特定标记的类别,javascript,google-maps,google-maps-api-3,google-maps-markers,markerclusterer,Javascript,Google Maps,Google Maps Api 3,Google Maps Markers,Markerclusterer,使用MarkerClusterer,我只想对一类标记进行聚类。 我这里有10个记号笔。我只想使用一个“type”变量,即“cluster”或“nocluster”,对其中的8个进行集群 听起来很简单,但我没有找到任何提示。。。 有什么想法吗 多谢各位 <script> function initMap() { var map = new google.maps.Map(document.getElementById('map'), { zoom

使用MarkerClusterer,我只想对一类标记进行聚类。 我这里有10个记号笔。我只想使用一个“type”变量,即“cluster”或“nocluster”,对其中的8个进行集群

听起来很简单,但我没有找到任何提示。。。 有什么想法吗

多谢各位

<script>

    function initMap() {

      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 8,
        center: {
          lat: 48.371310,
          lng: 7.593634
        },
        gestureHandling: 'cooperative'
      });

      var markers1 = locations.map(function(location, i) {

        var marker = new google.maps.Marker({
          position: location,
          type: location.type
        });

      });

      // Add a marker clusterer to manage the markers. 
      var mcOptions = {gridSize: 50, maxZoom: 10, styles: [{
        anchor:[0,0],
        textSize: 14,
        height: 50,
        width: 50
      }]
    };

      var markerCluster = new MarkerClusterer(map, markers1, mcOptions);

    } /* end FUNCTION initMap */

    var locations = [
            {num: '1', type: 'cluster', lat: 49.050288, lng: 7.950412},
            {num: '2', type: 'cluster', lat: 48.929413, lng: 7.852254},
            {num: '3', type: 'cluster', lat: 48.926529, lng: 7.361955},
            {num: '4', type: 'cluster', lat: 48.892072, lng: 7.655839},
            {num: '5', type: 'cluster', lat: 48.887685, lng: 7.785195},
            {num: '6', type: 'cluster', lat: 48.857382, lng: 7.321078},
            {num: '7', type: 'cluster', lat: 48.856634, lng: 7.319182},
            {num: '8', type: 'cluster', lat: 48.761871, lng: 7.967141},
            {num: '9', type: 'nocluster', lat: 48.736924, lng: 7.709988},
            {num: '10', type: 'nocluster', lat: 48.749944, lng: 7.340100}
          ];

    google.maps.event.addDomListener(window, "load", initMap);

</script>

<script src="js/markerclusterer.js"></script>

函数initMap(){
var map=new google.maps.map(document.getElementById('map'){
缩放:8,
中心:{
拉脱维亚:48.371310,
液化天然气:7.593634
},
手势处理:“合作”
});
var markers1=locations.map(函数(位置,i){
var marker=new google.maps.marker({
位置:位置,,
类型:location.type
});
});
//添加标记群集器以管理标记。
var mcOptions={gridSize:50,maxZoom:10,样式:[{
锚定:[0,0],
文本大小:14,
身高:50,
宽度:50
}]
};
var markerCluster=新的MarkerClusterer(map、markers1、mcOptions);
}/*端函数initMap*/
变量位置=[
{num:'1',type:'cluster',lat:49.050288,lng:7.950412},
{num:'2',type:'cluster',lat:48.929413,lng:7.852254},
{num:'3',type:'cluster',lat:48.926529,lng:7.361955},
{num:'4',type:'cluster',lat:48.892072,lng:7.655839},
{num:'5',type:'cluster',lat:48.887685,lng:7.785195},
{num:'6',type:'cluster',lat:48.857382,lng:7.321078},
{num:'7',type:'cluster',lat:48.856634,lng:7.319182},
{num:'8',type:'cluster',lat:48.761871,lng:7.967141},
{num:'9',type:'nocluster',lat:48.736924,lng:7.709988},
{num:'10',type:'nocluster',lat:48.749944,lng:7.340100}
];
google.maps.event.addDomListener(窗口“加载”,initMap);

这很简单-如果您希望对标记进行聚类,则将其添加到
markerCluster
,否则,只需使用
marker.setMap(map)

google.maps.Marker
将忽略您传递的
类型
键,因为它与它没有任何关系(此外,没有检索它的机制),因此您可能只需要创建两组标记:

var markersToCluster = [];
var markersToNotCluster = [];
locations.forEach(function(location, i) {
   var marker = new google.maps.Marker({position:location});
   location.type === 'something' ? markersToCluster.push(marker) : markersToNotCluster.push(marker);
});

var markerCluster = new MarkerClusterer(map, markersToCluster, mcOptions);
markersToNotCluster.forEach(function(m) { m.setMap(map); });