openlayers限制地圖拖動區域
阿新 • • 發佈:2019-01-05
現在做webgis基本都會用到openlayers或者leaflet。那麼在顯示地圖的時候,特別是顯示小區域地圖的時候,由於地圖區域較小,就會存在把地圖拖動到顯示區域之外的現象。那麼該如何限制地圖拖動的區域呢。
在openlayers2中有restrictedExtent屬性,直接設定下即可。
[javascript] view plain copy- var options = {
- controls : [
- new OpenLayers.Control.Navigation()
-
],
- projection: new OpenLayers.Projection("EPSG:4326"),
- maxResolution: 0.703125,
- // minScale:1/3500,
- // minScale:100,
- numZoomLevels :22 ,
- restrictedExtent: restrictedExtent,
- //maxExtent: new OpenLayers.Bounds(-180, -90, 180, 90),
-
//allOverlays : true
- };
- //初始化map物件
- this.map = new OpenLayers.Map('map',options);
但是在openlayers3中,由於沒有直接的屬性和介面使用,因此需要用別的方法。我用的方法就是在map的view物件中新增extent屬性來限制。
[javascript] view plain copy
- map.setView(new ol.View({
- center: mapCenter,
- projection: this.projection,
-
//extent : mapExtent,
- zoom: 18,
- minZoom: 16,
- maxZoom: 23,
- extent:[mapExtent[1]-0.0001,mapExtent[0]-0.0001,mapExtent[3]+0.0001,mapExtent[2]+0.0001]
- }));