0
votes

Leaflet zooms so now and than too much on clicking a cluster. A small part of 1 marker is shown on the right and a small part from the other marker is shown on the left side of the map.

My workaround is counting markers in scope and re-adjust the zoomlevel when the counter = 0. Shouldn't that be in the zoomcalculation of leaflet?

The zoom correction:

    mcg.on('clusterclick', function () {
         if (markersInScope == 0) {
             var zoom = map.getZoom();
             map.setZoom(zoom - 1);     
       }
    }); 
2
Please make sure to provide enough code to reproduce your issue (see MCVE). From what you describe, that would probably mean a map with width and height and the coordinates of at least the 2 said Markers.ghybs

2 Answers

2
votes

You could try adjusting the zoom to fit the bounds of the mcg and add some padding

mcg.on('clusterclick', function () {
         map.fitBounds(mcg.getBounds().pad(0.5));     
       }
    });
1
votes

If you add the markers to a featureGroup, the bounds for the group will available to you:

Here is a sample file from Leaflet:

<!DOCTYPE html>
<html>
<head>
    <title>Leaflet debug page</title>

    <link rel="stylesheet" href="../../dist/leaflet.css" />

    <link rel="stylesheet" href="../css/screen.css" />

    <script src="../leaflet-include.js"></script>
</head>
<body>

    <div id="map" style="width: 600px; height: 600px; border: 1px solid #ccc"></div>
    <button onclick="geojsonLayerBounds();">Show GeoJSON layer bounds</button>
    <button onclick="featureGroupBounds();">Show feature group bounds</button>

    <script src="geojson-sample.js"></script>
    <script>

        var osmUrl = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
            osmAttrib = '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
            osm = L.tileLayer(osmUrl, {maxZoom: 18, attribution: osmAttrib}),
            rectangle,
            featureGroup;

        var map = new L.Map('map', {
            center: new L.LatLng(0.78, 102.37),
            zoom: 7,
            layers: [osm]
        });

        var geojson = L.geoJson(geojsonSample, {

            style: function (feature) {
                return {color: feature.properties.color};
            },

            onEachFeature: function (feature, layer) {
                var popupText = 'geometry type: ' + feature.geometry.type;

                if (feature.properties.color) {
                    popupText += '<br/>color: ' + feature.properties.color
                }

                layer.bindPopup(popupText);
            }
        });

        geojson.addLayer(new L.Marker(new L.LatLng(2.745530718801952, 105.194091796875)))

        var eye1 = new L.Marker(new L.LatLng(-0.7250783020332547, 101.8212890625));
        var eye2 = new L.Marker(new L.LatLng(-0.7360637370492077, 103.2275390625));
        var nose = new L.Marker(new L.LatLng(-1.3292264529974207, 102.5463867187));
        var mouth = new L.Polyline([
            new L.LatLng(-1.3841426927920029, 101.7333984375),
            new L.LatLng(-1.6037944300589726, 101.964111328125),
            new L.LatLng(-1.6806671337507222, 102.249755859375),
            new L.LatLng(-1.7355743631421197, 102.67822265625),
            new L.LatLng(-1.5928123762763, 103.0078125),
            new L.LatLng(-1.3292264529974207, 103.3154296875)
        ]);
        map.addLayer(eye1).addLayer(eye2).addLayer(nose).addLayer(mouth);
        featureGroup = new L.FeatureGroup([eye1, eye2, nose, mouth]);

        map.addLayer(geojson);
        map.addLayer(featureGroup);

        function geojsonLayerBounds() {
            if (rectangle) {
                rectangle.setBounds(geojson.getBounds());
            } else {
                rectangle = new L.Rectangle(geojson.getBounds());
                map.addLayer(rectangle);
            }
        }

        function featureGroupBounds() {
            if (rectangle) {
                rectangle.setBounds(featureGroup.getBounds());
            } else {
                rectangle = new L.Rectangle(featureGroup.getBounds());
                map.addLayer(rectangle);
            }

        }
    </script>
</body>
</html>