Get bounds of featureGroup with multiple features

by Dixos   Last Updated April 25, 2018 16:22 PM

I tried looking into a solution for this myself, or an alternative way to load my layers, but I'm simply unable to.

Right now I load a geoJSON file through L.GeoJSON.AJAX(), then add the features (polylines and markers) that I need to a L.featureGroup(), but I'm having issues using the featureGroup.getBounds() function when doing this.

How can I get the bounds of all the features added to a featureGroup like this?

Edit: Added code. Completely slipped my mind when asking the question to include this.

function addMapData(id, map) {
  var newIcon = L.icon({
    iconUrl: './img/icons/ring.png',
    iconSize: [84, 80],
    iconAnchor: [5, 70],
    popupAnchor: [10, 10],
  });

  var mapData = L.featureGroup();

  new L.GeoJSON.AJAX('./data/fjelltrimmen/fjelltrimmen.vefsn.geojson', {
    local: true,
    style: {
      weight: 5,
      color: '#c0392b',
    },
    onEachFeature: function(feature, layer) {
      if (feature.properties.id === id) {
        // Polyline
        layer.addTo(mapData);

        // Map marker
        L.marker([
          feature.geometry.coordinates[feature.geometry.coordinates.length - 1][1],
          feature.geometry.coordinates[feature.geometry.coordinates.length - 1][0],
        ], { icon: newIcon }).addTo(mapData);
      }
    }
  });

  mapData.addTo(map);
  map.fitBounds(mapData.getBounds());   
}


Related Questions



Looking for world maritime boundaries in Geojson

Updated January 08, 2018 16:22 PM

European postal code shapes provider

Updated February 26, 2017 17:22 PM

Row-crop field boundaries

Updated March 31, 2018 21:22 PM