I have built a simple map using OpenLayers 3, have a look at the code below:
JavaScript code:
var map;
function init(){
map = new ol.Map({
target:'map',
renderer:'canvas',
view: new ol.View({
projection: 'EPSG:900913',
center:[-8015003.33712,4160979.44405],
zoom:5
})
});
var newLayer = new ol.layer.Tile({
source: new ol.source.OSM()
});
map.addLayer(newLayer);
/*
get and display extent
http://gis.stackexchange.com/questions/168590/how-to-get-extent-in-openlayers-3
*/
var size = map.getView().calculateExtent(map.getSize());
console.log(size);
console.log(map.getSize());
/* ========================== */
}
/* function to remove layer */
function removeTopLayer(){
var layers = map.getLayers();
layers.pop();
}
/* ========================= */
/* swap layers function */
function swapTopLayer(){
var layers = map.getLayers();
var topLayer = layers.removeAt(2);
layers.insertAt(1, topLayer);
}
/* =================== */
HTML code:
<body onload="init()">
<!--[if lt IE 8]>
<p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<!-- Add your site or application content here -->
<div id='map'></div>
<button style='position:absolute; left: 10px; top:30%' onclick='removeTopLayer()'>Remove Top Layer</button>
<button style='position:absolute; left: 10px; top:40%' onclick='swapTopLayer()'>Swap Top Layers</button>
</body>
Functioning link can be found HERE.
Now I wanted to add a bounding box, so that when the map loads, it only loads for a specific area, I searched for examples online, but they are either of OL2 or are about a user selecting a bound box dynamically, which is not my requirement. My requirement is pretty simple, all I want is the tiles for a specific bounding box to load instead of the map for the entire world to load.
I found the below method in OpenLayers 3.0 doc's boundingExtent, but I am not sure how to use it. :( So now suppose I want to add a bounding box for THIS area, how would I go about doing it, using the boundingExtent method? Can anybody explain?
I have been scratching my head over this one for quite sometime now, can anybody help me achieve my goal of have only a specific bounding box load?