问题
I was trying to implement MarkerCluster (MS) with Overlapping Marker Cluster (OMS). Everything seems to work just fine.
However, I am looking to modify the way OMS is works so that if I click on a cluster that has 2 points under it
When I click on any Cluster with 2 points in it with exact same geo location, its opens a marker and when I am clicking on that marker it's opening spider with 2 markers.
What I want when I am clicking on the Cluster, straight a way it will open up spider with 2 markers, already spend lot of times but still nothing worked.
I already tried many solutions, like
1. I can track the marker when I am adding to OMS(oms.addMarker), and can click depending on zoom_changed event of google map, but its not firing spiderfy rather its firing click event of what we added to markers.....
2. I have see a event spiderfy, so I tried to trigger that event with a marker object (oms.trigger('spiderfy', marker);) but nothing working...
Here I am adding code snippet too:
mc = new MarkerClusterer(map, markers.locations, mcOptions);
google.maps.event.addListener(mc, 'clusterclick', function(cluster) {
enter code hereclusterClicked = true;
// HERE WE WANTS TO FIRE SPIDER FUNCTIONALITY ...
});
回答1:
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.
- We capture the click event of markerCluster.
- obtain the markers of markerCluster.
- check whether all the markerCluster markers are in the same position.
- 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;
}
}
来源:https://stackoverflow.com/questions/15549797/clicking-on-marker-cluster-wants-to-open-spider-with-exact-same-location-markers