Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
OpenLayers 3.x Cookbook

You're reading from   OpenLayers 3.x Cookbook This book will provide users with a variety of recipes that illustrate different features present in OpenLayers 3

Arrow left icon
Product type Paperback
Published in Mar 2016
Publisher
ISBN-13 9781785287756
Length 304 pages
Edition 2nd Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Antonio Santiago Antonio Santiago
Author Profile Icon Antonio Santiago
Antonio Santiago
Peter J. Langley Peter J. Langley
Author Profile Icon Peter J. Langley
Peter J. Langley
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Web Mapping Basics FREE CHAPTER 2. Adding Raster Layers 3. Working with Vector Layers 4. Working with Events 5. Adding Controls 6. Styling Features 7. Beyond the Basics Index

Restricting the map's extent

Often, there are situations where you are interested in showing data to the user, but only for a specific area, which your available data corresponds to (a country, a region, a city, and so on).

In this case, there is no point in allowing the user to explore the whole world, so you need to limit the extent the user can navigate.

In this recipe, we present some ways to limit the area that a user can explore. You can find the source code in ch01/ch01-map-extent/. We'll end up with a restricted extent of the USA like in the following screenshot:

Restricting the map's extent

How to do it…

  1. Create the HTML to house the map and include the OpenLayers dependencies.
  2. Create your JavaScript file and set up a geographic extent:
    var extent = ol.proj.transformExtent(
      [-125.0011, 24.9493, -66.9326, 49.5904],
      'EPSG:4326', 'EPSG:3857'
    ); 
  3. Create the map instance with some layers and a restricted view, as follows:
    new ol.Map({
      layers: [
        new ol.layer.Tile({
          source: new ol.source.Stamen({
            layer: 'watercolor'
          })
        }),
        new ol.layer.Tile({
          source: new ol.source.Stamen({
            layer: 'terrain-labels'
          }),
          extent: extent
        })
      ],
      target: 'js-map',
      view: new ol.View({
        zoom: 6,
        minZoom: 5,
        center: [-12100000, 3400000],
        extent: extent
      })
    });

How it works…

When you launch this recipe on your web browser, you'll notice that you cannot pan outside the restricted extent. Let's take a look at how this was accomplished:

var extent = ol.proj.transformExtent(
  [-125.0011, 24.9493, -66.9326, 49.5904],
  'EPSG:4326', 'EPSG:3857'
);

We've put together a bounding box which covers the United States. This extent is in longitude and latitude coordinates, but the map view is in a different projection (EPSG:3857). We need to convert our longitude and latitude extent into the map view projection.

The ol.proj.transformExtent projection helper method provides such a utility. We pass in the array of coordinates as the first parameter. The second parameter informs OpenLayers that the provided coordinates are in longitude and latitude (EPSG:4326). The final parameter tells OpenLayers what we'd like the coordinates to be converted into (EPSG:3857). This returns with an ol.Extent array we can use on the map. We store this array in a variable, namely extent, as we'll use it in a few places around the code:

new ol.Map({
  layers: [
    new ol.layer.Tile({
      source: new ol.source.Stamen({
        layer: 'watercolor'
      })
    }),
    new ol.layer.Tile({
      source: new ol.source.Stamen({
        layer: 'terrain-labels'
      }),
      extent: extent
    })
  ],

When we create the new map instance, we make use of the Stamen tile services. The background layer is made up of the watercolor layer, and the foreground layer is made up from the terrain-labels layer.

For the terrain-labels layer, we restrict the extent of the layer with our custom bounding box. It means that this layer will not request for tiles outside this extent.

view: new ol.View({
  zoom: 6,
  minZoom: 5,
  center: [-12100000, 3400000],
  extent: extent
})

When we create the view, we pass our bounding box into the extent property of the view. Passing the extent to view is where the navigation restriction gets enforced. If we hadn't passed the extent to view, the user could pan around the map as they wish.

We also set minZoom to 5, which accompanies the extent restriction quite well. It prevents the user from zooming far out and beyond the USA (our extent). This retains the user within the points of interest.

See also

  • The Moving around the map view recipe
You have been reading a chapter from
OpenLayers 3.x Cookbook - Second Edition
Published in: Mar 2016
Publisher:
ISBN-13: 9781785287756
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image