infoWindow on MarkerClusterer in google maps

ぐ巨炮叔叔 提交于 2019-12-08 04:50:20

问题


I need infoWindow to be opened instead of zooming in map, when clicking on the ClusterMarker. I am using Gmaps util library MarkerClusterer for creating cluster of markers. I tried changing following line in markerclusterer.js

ClusterMarker_.prototype = new GOverlay();

with

ClusterMarker_.prototype = new GMarker();

so that I can get the openInfoWindow() function in the clustermarker, but that didnt worked out. Got some error. If possible, Please suggest solution so that this can be done with MarkerClusterer. Or else any other library which will be able to do this. Any help will be appreciated.


回答1:


You are probably better off modifying the click event for the marker in markerclusterer.js starting on line 672.

Currently:

  GEvent.addDomListener(div, "click", function () {
    var pos = map.fromLatLngToDivPixel(latlng);
    var sw = new GPoint(pos.x - padding, pos.y + padding);
    sw = map.fromDivPixelToLatLng(sw);
    var ne = new GPoint(pos.x + padding, pos.y - padding);
    ne = map.fromDivPixelToLatLng(ne);
    var zoom = map.getBoundsZoomLevel(new GLatLngBounds(sw, ne), map.getSize());
    map.setCenter(latlng, zoom);
  });

Change to something like:

  GEvent.addDomListener(div, "click", function () {
    map.openInfoWindowHtml(latlng, "Put your infowindow content here");
  });

Obviously, depending on how much you want to abstract things, you could do a couple of things:

  • Add configuration options to MarkerClusterer to specify whether to do zoom in functionality or infowindow functionality
  • Define a callback function setup where you specify what function MarkerClusterer will call when a cluster is clicked.



回答2:


For MarkerCluster v3 there is a custom event named 'clusterclick', which return the markerCluster object, then you can get its center and assign it to an infoWindow, something like this:

google.maps.event.addListener(mc, 'clusterclick', function (mCluster) {
     //infowindow must be declared before in your code
     infowindow.setContent("your info");
     var myLatlng = new google.maps.LatLng(mCluster.getCenter().ya, mCluster.getCenter().za);
     infowindow.setPosition(myLatlng);
     infowindow.open(map);
});

Also you have to set the zoomOnClick option on false:

var mcoptions = { zoomOnClick: false, showText: true, averageCenter: true}
var mc = new MarkerClusterer(map, markersArray, mcoptions);


来源:https://stackoverflow.com/questions/2881090/infowindow-on-markerclusterer-in-google-maps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!