how can i Restrict map to show only the selected country?

℡╲_俬逩灬. 提交于 2020-01-14 14:52:07

问题


I am trying to make web map application with openlayers 3. I have a problem which i want to fix.

I am loading OSM layer as my base map of the application. But the problem with the OSM layer is that it shows whole world and I can pan my map all around the world.

I want my application to be fixed in certain part. I have map extent set but still it doesn't work.

I am using minZoom but it doesn't help.

Is there any other way to fix this prolem?

var centerpos = [84.2, 28.2];
var newpos = ol.proj.transform(centerpos,'EPSG:4326','EPSG:900913');

var baseLayerOSM = new ol.layer.Tile({  
source: new ol.source.MapQuest({
    layer: 'osm'
}),
isBaseLayer:true    
});

var map = new ol.Map({
  layers: [baseLayerOSM],
  target: 'map',
  controls: [new CustomControl()],
  view: new ol.View({
    extent:[80.05844110726194,26.34796712822462,88.2015218371264,30.44742963310623],
    projection : 'EPSG:900913', // OSM projection
    center : newpos,
    minZoom:7,
    zoom: 7
  })
});

This is my code.

AJ


回答1:


Basically your example should work, but I think you forgot to transform your extent to EPSG:900913 / EPSG:3857 as well.

var maxExtent = [80.05844110726194,26.34796712822462,88.2015218371264,30.44742963310623];

var map = new ol.Map({
  layers: [baseLayerOSM],
  target: 'map',
  controls: [new CustomControl()],
  view: new ol.View({
    extent: ol.proj.transformExtent(maxExtent, 'EPSG:4326', 'EPSG:900913'),
    projection : 'EPSG:900913', // OSM projection
    center : newpos,
    minZoom:7,
    zoom: 7
  })
});


来源:https://stackoverflow.com/questions/32003073/how-can-i-restrict-map-to-show-only-the-selected-country

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!