[GIS] How to fit an OpenLayers view to an extent that spans the map extent (wrap) or IDL

openlayersopenlayers-2

How to fit the view to an extent that spans the map extent (wrap-point) or IDL?

Please excuse my semantics if they are incorrect, I'm new to GIS.

I've set up some code that does a zoom to extent based on these OpenLayers examples:

http://openlayers.org/en/latest/examples/center.html

http://openlayers.org/en/latest/examples/draw-features.html

The code (see below), works fine for extents that stay within the extents of the world map, e.g. Africa.

It does not work as I would like for my Pacific region, which spans the IDL and the point at which the map "wraps" in OpenLayers.

The actual behaviour is that the line between the 2 points goes "the long way", showing me almost the whole map, instead of the short way which gives only the Pacific. See screenshots after code sample.

var Pacific = {
            bottomLeft: [112.67578, -49.42705],
            topRight: [-125.85937, 45.78093]
        };

var Africa = {
            bottomLeft: [21.62109, -33.92741],
            topRight: [60.11719, 37.80327]
        };

var region = Pacific; //Africa;

myLib.zoomToExtent(region.bottomLeft, region.topRight, function (){;});

zoomToExtent: function (bottomLeftLonLat, topRightLonLat, onZoomCompleteCallback) {
        var map = _getMap(_domTarget);
        var view = map.getView();

        var size = /** @type {ol.Size} */ (map.getSize());

        var ptA = ol.proj.fromLonLat(bottomLeftLonLat, _defaultProjection);
        var ptB = ol.proj.fromLonLat(topRightLonLat, _defaultProjection);

        geometry = new ol.geom.Polygon(null);

        var start = ptA;
        var end = ptB;

        geometry.setCoordinates([
          [start, [start[0], end[1]], end, [end[0], start[1]], start]
        ]);

        view.fit(geometry, size, { padding: [1, 1, 1, 1], constrainResolution: false });

        _numTilesLoaded = 0;
        _setBaseLayerLoadedCallback(map, onZoomCompleteCallback);
}

Actual effect of the above code (not exact, for example):

Actual effect

Desired effect (not exact, for example):

Desired effect

Best Answer

I don't know if it's the correct way, but OL can simulate latitudes over 180 and below -180. Eg. you can modify the right longitude of your pacific region to 360-125.85937.

At least this works with extents. Here are examples:

Fitting to the West

Fitting to the East