I know, how to add an overlay layer by specifying my own URL. But what if the overlay layer extent is not as large as base layer - e.g. overlay layer is only avaliable for one country and not for the whole world.

  new OpenLayers.Layer.OSM("My custom local overlay layer",

How can I pass parameter, that OL would not seek for the tiles, that does not exist - that are outside of the avaliable layer extent (outside that country).

Thank you,



There are two approaches that could be used. The first one is more like what you are trying to achieve.

Option 1: Layer max extent

You can set the maxExtent property for the layer along with the displayOutsideMaxExtent property to prevent OpenLayers from requesting tiles that would fall outside the maximum extent. If you don't specify a maximum extent for a layer then it adopts the extent of the base layer. You could change your code to create the layer to include these properties:

new OpenLayers.Layer.OSM("My custom local overlay layer",
  maxExtent: new OpenLayers.Bounds([left, bottom, right, top]),
  displayOutsideMaxExtent: false

If the user zooms or pans outside the maxExtent for the layer the OpenLayers will not request tiles.

Option 2: Pan and zoom restriction

You simply need to set the restrictedExtent property for your map. This can either be an OpenLayers.Bounds object or an Array. If you specify an Array then it must be of the form [left, bottom, right, top]. For example;

map.restrictedExtent(new OpenLayers.Bounds(left, bottom, right, top));


map.restrictedExtent(new OpenLayers.Bounds([left, bottom, right, top]));


map.restrictedExtent([left, bottom, right, top]);

In each case map being a reference to your map. The default for restrictedExtent is null in which case OpenLayers uses the extents of the base layer. Setting restrictedExtent overrides this behaviour and instead OpenLayers will not allow zooming or panning outside the specified extents.

  • Hmm. But I want to be able to display whole world, and put overlay only over that area. Doesn't restrictedExtent restrict panning a map outsite that area? – Matej Sep 19 '13 at 23:02
  • Ah, I see what you mean. I have updated my answer to include that use case too. – CHenderson Sep 20 '13 at 1:31

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.