Leaflet: Any easy method to change center and zoom to fit a polygon layer

Created on 4 Nov 2011  路  2Comments  路  Source: Leaflet/Leaflet

In OpenLayers, you can do something slick like this:

var poly = wkt.read(' ... ');
var bounds = poly.geometry.getBounds();
map.zoomToExtent(bounds);

Is there a way to make that happen in Leaflet?

Most helpful comment

Ah, I think figured it out. RTFM, jerkus. How's something like this look?

var southWest = new L.LatLng(34.0177295716, -118.470884195),
    northEast = new L.LatLng(34.0524049939, -118.430298),
    bounds = new L.LatLngBounds(southWest, northEast);
map.fitBounds(bounds);

And you could dynamically fill in those corners, and I think you'd be flying.

All 2 comments

Ah, I think figured it out. RTFM, jerkus. How's something like this look?

var southWest = new L.LatLng(34.0177295716, -118.470884195),
    northEast = new L.LatLng(34.0524049939, -118.430298),
    bounds = new L.LatLngBounds(southWest, northEast);
map.fitBounds(bounds);

And you could dynamically fill in those corners, and I think you'd be flying.

I didn't yet document it but you can also do polyline.getBounds(). As for WKT, there's no Leaflet parser for it but it would be quite easy to write, so I think such plugin will appear soon.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timwis picture timwis  路  3Comments

piehei picture piehei  路  3Comments

prbaron picture prbaron  路  3Comments

zdila picture zdila  路  3Comments

brambow picture brambow  路  3Comments