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 Cookbook

You're reading from   OpenLayers Cookbook The best method to learn the many ways OpenLayers can be used to render data on maps is to dive straight into these recipes. With a mix of basic and advanced techniques, it's ideal for JavaScript novices and experts alike.

Arrow left icon
Product type Paperback
Published in Aug 2012
Publisher Packt
ISBN-13 9781849517843
Length 300 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Table of Contents (15) Chapters Close

OpenLayers Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Web Mapping Basics FREE CHAPTER 2. Adding Raster Layers 3. Working with Vector Layers 4. Working with Events 5. Adding Controls 6. Theming 7. Styling Features 8. Beyond the Basics Index

Restricting the map extent


Often, there are situations where you are interested to show 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 a user can explore.

How to do it...

  1. Create a map instance. Take a look at the couple of properties used in the constructor:

    var map = new OpenLayers.Map("ch1_restricting_view", {
        maxExtent: OpenLayers.Bounds.fromString("-180,-90,180,90"),
        restrictedExtent: OpenLayers.Bounds.fromString("-180,-90,180,90")
    });
    
  2. As always, add some layer to see content and center the view:

    var wms = new OpenLayers.Layer.WMS("OpenLayers WMS Basic", "http://vmap0.tiles.osgeo.org/wms/vmap0",
    {
        layers: 'basic'
    });
    map.addLayer(wms);
    map.setCenter(new OpenLayers.LonLat(0, 0), 2);
  3. Add the functions that will be executed when buttons are clicked:

    function updateMaxExtent() {
        var left = dijit.byId('left_me').get('value');
        var bottom = dijit.byId('bottom_me').get('value');
        var right = dijit.byId('rigth_me').get('value');
        var top = dijit.byId('top_me').get('value');      
        map.setOptions({
            maxExtent: new OpenLayers.Bounds(left, bottom, right, top)
        });
    }
    function updateRestrictedExtent() {
        var left = dijit.byId('left_re').get('value');
        var bottom = dijit.byId('bottom_re').get('value');
        var right = dijit.byId('rigth_re').get('value');
        var top = dijit.byId('top_re').get('value');
        map.setOptions({
            restrictedExtent: new OpenLayers.Bounds(left, bottom, right, top)
        });
    }

How it works...

As you have seen, the map has been instantiated using the two properties maxExtent and restrictedExtent, which are responsible for limiting the area of the map we can explore.

Although similar, these two properties have different meanings. Setting the maxExtent property limits the viewport so its center cannot go outside the specified bounds. By setting the restrictedExtent property the map itself cannot be panned beyond the given bounds.

The functions that react when buttons are clicked get the values from the input fields and apply the new values through the map.setOptions() method:

map.setOptions({
    maxExtent: new OpenLayers.Bounds(left, bottom, right, top)
});

We can pass the same properties we use when creating a new OpenLayers.Map instance to the map.setOptions() method and it will take care to update them.

There's more...

Limiting the map extent is not the only way to limit the information we show to the user. The layers have also similar properties to filter or limit the information they must render.

See also

  • The Moving around the map view recipe

You have been reading a chapter from
OpenLayers Cookbook
Published in: Aug 2012
Publisher: Packt
ISBN-13: 9781849517843
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