JavaScript Leaflet.js - Fit geoJSON co-ordinates on map view

I have a leaflet.js map that has points and linestrings on it that come from an external JSON file.

If I add: map.setView(new L.LatLng(0,0), 10);

It will centre the map on the latitude and longitude 0,0. How can I set it so the map centre and zoom fit all of the points from the JSON on it?

Answer:1

You could add all of your layers to a FeatureGroup which has a getBounds method. So you should be able to just say myMap.fitBounds(myFeatureGroup.getBounds());

The getBounds method for L.FeatureGroup is only available in the master branch (not the latest version, 0.3.1), for now at least.

Answer:2

Similar case with me. I drawn all the markers from GeoJson data. So I written the function, which gets called repeatedly on button click. Just try if it suits your requirements.

   function bestFitZoom()
    {
        // declaring the group variable  
        var group = new L.featureGroup;

        // map._layers gives all the layers of the map including main container
        // so looping in all those layers filtering those having feature   
        $.each(map._layers, function(ml){

           // here we can be more specific to feature for point, line etc.            
            if(map._layers[].feature) 
             {
                 group.addLayer(this)
             }
         })

         map.fitBounds(group.getBounds());
    }

The best use of writing this function is that even state of map/markers changed, it will get latest/current state of markers/layers. Whenever this method gets called all the layers will be visible to modest zoom level.

Answer:3

I have to create an interface similar to what http://www.madeiracloud.com provides for drawing a network architecture (for different purposes than what they are doing). Basically users should be able ...

I have to create an interface similar to what http://www.madeiracloud.com provides for drawing a network architecture (for different purposes than what they are doing). Basically users should be able ...

I'm having somewhat of an odd issue with trying to piece together a somewhat dynamic Google Maps display. I have overlays on a map that I would like to call a function when clicked. Initially I had ...

I'm having somewhat of an odd issue with trying to piece together a somewhat dynamic Google Maps display. I have overlays on a map that I would like to call a function when clicked. Initially I had ...

First of all look at this question. None of those libraries support Multigraphs (or Pseudographs ). I mean I can't generate graphs like this: Is there any jQuery plugin (or javascript library) out ...

First of all look at this question. None of those libraries support Multigraphs (or Pseudographs ). I mean I can't generate graphs like this: Is there any jQuery plugin (or javascript library) out ...

I have a paragraph element inside a div. The div has an opacity of 0.3 & the paragraph has an opacity of 1. When I show the elements, it appears the paragraph is transparent, like it has an ...

I have a paragraph element inside a div. The div has an opacity of 0.3 & the paragraph has an opacity of 1. When I show the elements, it appears the paragraph is transparent, like it has an ...