javascript - Zooming an OpenLayers3 map to cover multiple vector layers -
javascript - Zooming an OpenLayers3 map to cover multiple vector layers -
i have openlayers3 map consisting of osm tile layer , 1 or more vector layers. can zoom map single layer using
vector.addeventlistener("change", function() { map.getview().fitextent(vectorsource.getextent(), map.getsize()); });
and works. however, if seek zoom cover multiple layers using next in loop adds layers
vector.addeventlistener("change", function () { if (bounds == null) { bounds = vectorsource.getextent(); } else { bounds = bounds.extend(vectorsource.getextent()); } map.getview().fitextent(bounds, map.getsize()); });
with bounds beingness declared outside loop, maps single layer go on work. however, more 1 layer, code in else block give error "undefined not function".
according documentation getextent() returns extent object , the extent object has extend method, i'm not sure why errors.
answering own question worked out. using extend wrongly. right code is:
vector.addeventlistener("change", function () { if (bounds == null) { bounds = vectorsource.getextent(); } else { bounds = ol.extent.extend(bounds, vectorsource.getextent()); } map.getview().fitextent(bounds, map.getsize()); });
javascript openlayers openlayers-3
Comments
Post a Comment