markerclusterer info windows

前端 未结 4 1867
遇见更好的自我
遇见更好的自我 2020-12-15 14:18

Let me say I am still fairly new to google maps and javascript. i\'ve been mixing together the google store locator tutorial with some other stuff. So far, I am using mark

4条回答
  •  猫巷女王i
    2020-12-15 14:40

    Below is how I solved that with Google Maps API v3 and Marker Clusterer. I've also addes some offset for the infoWindow so that it doesn't open in the center of the cluster, but little bit above of it.

    // Define map variables
    var map = new google.maps.Map(document.getElementById('mymap', {
      zoom: 9,
      center: {lat: 50, lng: 10}
    });
    var infoWindow = new google.maps.InfoWindow();
    var markerCluster = new MarkerClusterer(map, markers, {zoomOnClick: false});
    
    // Add listener for clusterclick event
    markerCluster.addListener('clusterclick', function(cluster) {
      // Optional: Set some offset for latitude,
      // so that the InfoWindow opens a bit above the cluster
      var offset = 0.1 / Math.pow(2, map.getZoom());
      infoWindow.setContent('
    Some content
    '); infoWindow.setPosition({ lat: cluster.center_.lat() * (1 + offset), lng: cluster.center_.lng() }); infoWindow.open(map); });

提交回复
热议问题