官术网_书友最值得收藏!

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:

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
主站蜘蛛池模板: 吉木萨尔县| 永州市| 屏东县| 通道| 湄潭县| 乌海市| 宁明县| 连江县| 大荔县| 霍邱县| 榕江县| 黄山市| 万安县| 阿尔山市| 普安县| 聂荣县| 西城区| 吉林市| 桦南县| 新乡市| 公主岭市| 浦江县| 盐山县| 永川市| 长顺县| 长宁县| 镇康县| 平山县| 陵川县| 仪陇县| 平南县| 四会市| 鸡西市| 湘潭市| 聊城市| 岚皋县| 沈丘县| 昔阳县| 墨竹工卡县| 柳江县| 五大连池市|