Wednesday 22 June 2016

javascript - OpenLayers 3 create bounding extent which includes two points


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?



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());

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...