Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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",
  ["http://a.tile.myserver.org/maps/${z}/${x}/${y}.png",
  "http://b.tile.myserver.org/maps/${z}/${x}/${y}.png",
  "http://c.tile.myserver.org/maps/${z}/${x}/${y}.png"]);

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,

Matej

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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",
["http://a.tile.myserver.org/maps/${z}/${x}/${y}.png",
"http://b.tile.myserver.org/maps/${z}/${x}/${y}.png",
"http://c.tile.myserver.org/maps/${z}/${x}/${y}.png"],
{
  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));

Or

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

Or

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.

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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