5

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.

8
  • What code do you currently have?
    – BDD
    Commented Oct 22, 2015 at 15:35
  • Added. I see that extent really just keeps the extent in view rather than actually bounding it to that view. Commented Oct 22, 2015 at 15:39
  • 1
    Might be a possible duplicate. Does this work?: gis.stackexchange.com/questions/60625/…
    – BDD
    Commented Oct 22, 2015 at 15:45
  • That is OL2, OL3 is completely different. Commented Oct 22, 2015 at 16:25
  • Perhaps remove the openlayers-2 tag? It would be clearer that it is not a duplicate, which it isn't. Commented Oct 22, 2015 at 16:31

2 Answers 2

6

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.

0
0

I ended up just using the bounds -180,75,180,-75 and accepting some dateline wrapping and not being able to use part of the north and south. EXTREMELY frustrating that there isn't a maxExtent in 3.10.1. Seems like a huge flaw. Map wrapping is ugly and so is having white space.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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