更改特定Google地图集群的集群图标-markerclusterplus

问题描述

我想根据某些特定条件更改群集图标(动态/针对特定群集)。我能够确定情况,并限制了进一步的深入研究。在该特定级别,我们正在为用户打开信息框。

问题在于群集,因为我想将图标更改为其他名称。对于单个图钉,图标就可以了。

下面是我要实现的代码:

<!DOCTYPE html>
<html>
   <head>
      <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: 80%;
         margin: 0;
         padding: 0;
         }
      </style>
   </head>
   <body>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCtTgoZ3tcsUSNfK6yCPzX_muSVOG6N9Ho"></script>
      <script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>
      <button type="button" id="refreshmap">Click Me!</button>
      <div id="map"></div>
      <script>
         var gm_map;
         var markerArray = [];
         var clusterMarkers = [];
         var infoWindow = new google.maps.InfoWindow();
         function initialize(searchMarkers = []) {
            var marker,i;
            
            if(searchMarkers.length == 0) {
                clusterMarkers = [
                    new google.maps.Marker({
                      position: new google.maps.LatLng(59.381059,13.504026),map: gm_map,title: "P1220214 1.JPG",label: {
                        text: 'Label 1',color: '#222222',fontSize: '12px'
                      }
                    }),new google.maps.Marker({
                      position: new google.maps.LatLng(59.381059,title: "P1220214 2.JPG",label: {
                        text: 'Label 2',fontSize: '12px'
                      }
                    
                    }),new google.maps.Marker({
                      position: new google.maps.LatLng(59.340715,13.49631),title: "P1220214 3.JPG",label: {
                        text: 'Label 3',new google.maps.Marker({
                      position: new google.maps.LatLng(59.327232,13.487384),title: "P1220214 4.JPG",label: {
                        text: 'Label 4',new google.maps.Marker({
                      position: new google.maps.LatLng(59.379034,13.516566),title: "P1220214 5.JPG",label: {
                        text: 'Label 5',new google.maps.Marker({
                      position: new google.maps.LatLng(59.330000,13.485000),title: "P1220214 6.JPG",label: {
                        text: 'Label 6',new google.maps.Marker({
                      position: new google.maps.LatLng(59.328501,13.485782),title: "P1220214 7.JPG",label: {
                        text: 'Label 7',title: "P1220214 8.JPG",label: {
                        text: 'Label 8',fontSize: '12px'
                      }
                    
                    })
                ]
            } else {
                clusterMarkers = searchMarkers;
            }
            
            var options_googlemaps = {
                minZoom: 9,zoom: 9,center: new google.maps.LatLng(59.328631,13.485688),maxZoom: 18,}
            
            gm_map = new google.maps.Map(document.getElementById('map'),options_googlemaps)
            
            var options_markerclusterer = {
                //gridSize: 20,zoomOnClick: false,imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m',label: {
                    text: 'This is custom text'
                }
            };
            
            var markerCluster = new MarkerClusterer(gm_map,clusterMarkers,options_markerclusterer);
            
            markerCluster.setCalculator(function(markers,numStyles){
                var index = 0,count = markers.length,total = count;
                
                while (total !== 0) {
                    //Create a new total by dividing by a set number
                    total = parseInt(total / 5,10);
                    //Increase the index and move up to the next style
                    index++;
                }
                index = Math.min(index,numStyles);
                samePin = _getPinStatus(markers);
                if (samePin) {
                    return {
                        text: count + " ("+ count + " Jobs)",index: index
                    };
                } else {
                    return {
                        text: count,index: index
                    };
                }               
            });
            
            
            google.maps.event.addListener(markerCluster,'clusterclick',function(cluster) {
                
                //This needs some logic to identify if all the internal pings are at same location - execute else part otherwise further zoom in using if code.
                var markers = cluster.getMarkers();
                var samePin = false;
                samePin = _getPinStatus(markers);
                
                if (!samePin ){
                    gm_map.setCenter(cluster.center_);
                    gm_map.setZoom(gm_map.getZoom() + 2);
                } else {
                    // This is where I want to change the icon of the particular cluster.   
                    var array = [];                     
                    for (i = 0; i < markers.length; i++) {
                        array.push(markers[i].getTitle() + '<br>');
                    }
                    if (gm_map.getZoom() <= markerCluster.getMaxZoom()) {
                        infoWindow.setContent(markers.length + " markers<br>" + array);
                        infoWindow.setPosition(cluster.getCenter());
                        infoWindow.open(gm_map);
                    }
                }
            });
            
            for (i = 0; i < clusterMarkers.length; i++) {
                var marker = clusterMarkers[i];
         
                google.maps.event.addListener(marker,'click',(function(marker) {
                    return function() {
                        infoWindow.setContent(this.getTitle());
                        infoWindow.open(gm_map,this);
                    }
                })(marker));
            }
         }
         
         $(document).ready(function() {
            initialize();
            $('body').on('click','#close-link',function() {
                $('#toggle-photolist').fadeOut();
                $('#close-overlay').fadeOut();
            });
            $('#refreshmap').click(function() {
                searchMarkers = [
                    new google.maps.Marker({
                      position: new google.maps.LatLng(59.381059,title: "Search Data 1.JPG",label: {
                        text: 'Search label 1',new google.maps.Marker({
                      position: new google.maps.LatLng(59.338683,13.492057),title: "Search Data 2.JPG",fontSize: '12px'
                      }
                    
                    })
                ]
                initialize(searchMarkers);
            });
         });
         
         function _getPinStatus(markers) {
            for (i = 0; i < markers.length; i++) {
                var parentLatitude = markers[0].getPosition().lat();
                var parentLongitude = markers[0].getPosition().lng();
                
                if(markers[i].getPosition().lat() == parentLatitude && 
                    markers[0].getPosition().lng() == parentLongitude) {                        
                    samePin = true;
                }
                else {
                    // break out of the loop in case a single pin is different and allow user to further zoom in the map
                    samePin = false;
                    break;
                }
            }
            return samePin;
         }
         
      </script>
      <!-- Replace following script src -->
   </body>
</html>

我要构建的逻辑是,如果群集下面有相同的引脚(我不想向下钻取),则将图标更改为其他任何内容。 函数_getPinStatus可帮助我识别以上逻辑并创建信息窗口。

我也以某种方式无法获得zoom change事件-但这是一个单独的查询

我尝试使用clearMarkers()函数,并通过提供options_markerclusterer数组中的图像来重新映射群集,但是它确实替换了所有基础标记上的图像。

我找不到有关setStyles及其实现的详细信息。地图绘制完成后,推送样式是否可行。

赞赏任何建议。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)