[GIS] Keep OpenLayers 3 map from going past the dateline

openlayers

I have an OL3 map that constantly repeats its self. I have contained the WMS layers, though the vector layers like to repeat. What is really important is keeping the user from panning into nowhere, i.e. Keeping the user from scrolling past the poles and dateline. I added an extent to the view but it still adds some map past the date line and poles. Any idea how to prevent this?

view: new ol.View({
        center: [0, 0],
        zoom: 8,
        minZoom:2,
        maxZoom: 13,
        extent: [-20037508.34, -20037508.34, 20037508.34, 20037508.34]

    })

Is my current code

Edit: The solutions from previous questions are all OpenLayers 2. OpenLayers 3 is a completely different framework that shares virtually nothing outside of the name.

Best Answer

To turn off wrapping, set wrapX to false for each layer source.

The extent configured on the view restricts the center of the map. To restrict the extent, you can do something like

map.on('postrender', function() {
  var view = map.getView();
  var zoom = view.getZoom();
  var worldExtent = view.getProjection().getExtent();
  var extent = view.calculateExtent(map.getSize());
  if (!ol.extent.containsExtent(worldExtent, extent)) {
    var newExtent = ol.extent.getIntersection(extent, restrictedExtent);
    view.fit(newExtent, map.getSize());
  }
});

Also see https://github.com/openlayers/ol3/pull/2777 for a pull request that will allow doing this in a nicer way.

Related Question