Clicking on marker cluster wants to open spider with exact same location markers by default

左心房为你撑大大i 提交于 2019-12-03 08:47:46

I could solve this problem, first of all it is obvious that you have implemented and Overlapping Xluster Marker Marker Cluster in your google maps.

My solution is something very simple.

  1. We capture the click event of markerCluster.
  2. obtain the markers of markerCluster.
  3. check whether all the markerCluster markers are in the same position.
  4. if they are in the same position we make a click event trigger the latter obtained the markerCluster marker.

In short this is the code:

       var markerClusterer = new MarkerClusterer(map, allMarkers, {styles: styles[0], clusterClass: 'poiCluster', maxZoom:18}); 
       google.maps.event.addListener(markerClusterer, 'click', function(cluster) {

        var markers = cluster.getMarkers();

        if(estanTodosEnLaMismaPosicion(markers)){
             //to wait for map update
            setTimeout(function(){
                google.maps.event.trigger(markers[markers.length-1], 'click');
            },1000)
        }



        return true;
    });




    function estanTodosEnLaMismaPosicion(markers){
    var cont=0;
    var latitudMaster=markers[0].getPosition().lat();
    var longitudMaster=markers[0].getPosition().lng();
    for(var i=0;i<markers.length;i++){
        if(markers[i].getPosition().lat() === latitudMaster & markers[i].getPosition().lng() === longitudMaster ){
            cont++;
        }else{
            return false;
        }
    }
    if(cont==markers.length){
        return true;
    }else if(cont<markers.length){
        return false;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!