Openlayers-限制地图拖动区域

现在做webgis基本都会用到openlayers或者leaflet。那么在显示地图的时候,特别是显示小区域地图的时候,由于地图区域较小,就会存在把地图拖动到显示区域之外的现象。那么该如何限制地图拖动的区域呢。   

 

    在openlayers2中有restrictedExtent属性,直接设置下即可。

  1. var options = {  
  2.             controls : [  
  3.                         new OpenLayers.Control.Navigation()  
  4.                         ],  
  5.             projection: new OpenLayers.Projection(“EPSG:4326”),  
  6.             maxResolution: 0.703125,  
  7.            // minScale:1/3500,  
  8. //            minScale:100,  
  9.             numZoomLevels :22 ,  
  10.             restrictedExtent: restrictedExtent,  
  11.             //maxExtent: new OpenLayers.Bounds(-180, -90, 180, 90),  
  12.             //allOverlays : true  
  13.     };  
  14.   
  15.     //初始化map对象  
  16.     this.map = new OpenLayers.Map(‘map’,options);  

 

但是在openlayers3中,由于没有直接的属性和接口使用,因此需要用别的方法。我用的方法就是在map的view对象中添加extent属性来限制。


    1. map.setView(new ol.View({  
    2.             center: mapCenter,  
    3.             projection: this.projection,  
    4.             //extent : mapExtent,  
    5.             zoom: 18,  
    6.             minZoom: 16,  
    7.             maxZoom: 23,  
    8.             extent:[mapExtent[1]-0.0001,mapExtent[0]-0.0001,mapExtent[3]+0.0001,mapExtent[2]+0.0001]  
    9.         })); 

转载自:https://blog.csdn.net/xiaohan2826/article/details/53859816

You may also like...