OpenLayers 3 – Create Bounding Extent Including Two Points

javascriptopenlayers

I'm working with openlayer3 in my ionic application and am stucked with the problem with creating a bounding extent to get a few points in the viewport.

My code looks like this:

var destLoc = [6.18581,50.88093];
var currentLoc = [-122.29925626,37.47836852];

var ext = ol.extent.boundingExtent([destLoc,currentLoc]);
map.getView().fit(ext,map.getSize());

But this code gives me a extent of the currentLoc and not the whole extent which includes both points.

What am I doing wrong?

Best Answer

Your bounding box is 100% correct.

But, presumably, your map is in EPSG:3857, so you can't use WGS coordinates directly. Transform it to 3857:

var ext = ol.extent.boundingExtent([destLoc,currentLoc]);
ext = ol.proj.transformExtent(ext, ol.proj.get('EPSG:4326'), ol.proj.get('EPSG:3857'));

map.getView().fit(ext,map.getSize());