Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do Bounds work in OpenLayers 3?

Does the concept of OpenLayers.Bounds from OpenLayers 2.x still exist in OpenLayers 3? How has it changed, and what is its new name?

like image 819
colllin Avatar asked Mar 05 '14 18:03

colllin


2 Answers

UPDATE: OL4: https://openlayers.org/en/latest/apidoc/ol.html#.Extent

It seems that the new word for 'bounds' or 'bounding box' (BBOX) is 'extent'. See:

  • http://openlayers.org/en/v3.20.1/apidoc/ol.extent.html
  • http://openlayers.org/en/v3.20.1/apidoc/ol.View.html#fitExtent
  • http://openlayers.org/en/v3.20.1/apidoc/ol.source.Vector.html#getExtent

One way to find out things at the moment is to run searches in the OL3 repo, for example: https://github.com/openlayers/ol3/search?p=3&q=BBOX&type=Code

like image 72
Christophe Roussy Avatar answered Sep 23 '22 00:09

Christophe Roussy


Did'nt found any documentation about this feature but Extent seems to work :

var vectorSources = new ol.source.Vector();
var map = new ol.Map({
  target: map_id,
  layers: [
    new ol.layer.Tile({
      source: ol.source.OSM()
    }),
    new ol.layer.Vector({
      source: vectorSources
    })
  ],
  view: new ol.View({
    center: [0, 0],
    zoom: 12
  })
});

var feature1 = new ol.Feature({
  geometry: new ol.geom.Point(coords)
});
vectorSources.addFeature(feature1);
var feature2 = new ol.Feature({
  geometry: new ol.geom.Point(coords)
});
vectorSources.addFeature(feature2);
map.getView().fitExtent(vectorSources.getExtent(), map.getSize());

The method vectorSources.getExtent() can also be replaced by any Extent object, like this :

map.getView().fitExtent([1,43,8,45], map.getSize());

Since OpenLayer 3.9, the method has changed :

map.getView().fit(vectorSources.getExtent(), map.getSize());

like image 35
Bastien Ho Avatar answered Sep 24 '22 00:09

Bastien Ho