3

https://openlayers.org/en/latest/examples/rotation.html is an example of a rotated map. If I take the extents of that, they do not reference the actual corners of the displayed map area but something prior(?) to rotation.

How do I get the actual extents or corner coordinates of a rotated map?

To see/reproduce the issue, you can copy and paste this into the browser console after loading above example and then zoom out.

var extents = map.getView().calculateExtent(map.getSize());
var poly_extents = ol.geom.Polygon.fromExtent(extents);

var p = new ol.layer.Vector({
  source: new ol.source.Vector({
      features: [new ol.Feature({
          geometry: poly_extents,
      })]
  }),
});

map.addLayer(p);

Extents of rotated map, shown after zooming out

1

An incredibly awkward and terrible way would be getting the coordinates of the outmost pixels via screen coordinates like this:

// from https://stackoverflow.com/a/11744120/4828720 , no idea if safe
var w = window,
    d = document,
    e = d.documentElement,
    g = d.getElementsByTagName('body')[0],
    x = w.innerWidth || e.clientWidth || g.clientWidth,
    y = w.innerHeight|| e.clientHeight|| g.clientHeight;

var topleft = map.getCoordinateFromPixel([0,0]);
var topright = map.getCoordinateFromPixel([x,0]);
var bottomleft = map.getCoordinateFromPixel([0,y]);
var bottomright = map.getCoordinateFromPixel([x,y]);

From there you could go on and create a Polygon or whatever:

var corner_coordinates = [topleft, topright, bottomright, bottomleft, topleft];
var bounding_geometry = new ol.geom.Polygon([coordinates]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.