【发布时间】:2014-03-05 18:48:48
【问题描述】:
OpenLayers 2.x 中OpenLayers.Bounds 的概念是否仍然存在于 OpenLayers 3 中?它发生了怎样的变化,它的新名称是什么?
【问题讨论】:
-
var mapExtent = map.getView().calculateExtent(map.getSize());
OpenLayers 2.x 中OpenLayers.Bounds 的概念是否仍然存在于 OpenLayers 3 中?它发生了怎样的变化,它的新名称是什么?
【问题讨论】:
更新:OL4:https://openlayers.org/en/latest/apidoc/ol.html#.Extent
'bounds' 或'bounding box' (BBOX) 的新词似乎是'extent'。 见:
目前找出问题的一种方法是在 OL3 存储库中运行搜索,例如: https://github.com/openlayers/ol3/search?p=3&q=BBOX&type=Code
【讨论】:
没有找到有关此功能的任何文档,但 Extent 似乎可以工作:
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());
vectorSources.getExtent() 方法也可以替换为任何 Extent 对象,如下所示:
map.getView().fitExtent([1,43,8,45], map.getSize());
从 OpenLayer 3.9 开始,方法发生了变化:
map.getView().fit(vectorSources.getExtent(), map.getSize());
【讨论】:
只是在答案中添加一个小例子: Bounds 现在被称为“范围”,它不再是一个复杂的对象/类,而只是一个由四个数字组成的数组。在“ol.extent”中有一堆用于转换等的辅助函数。只是一个关于如何进行转换的小例子:
罢工>
var tfn = ol.proj.getTransform('EPSG:4326', 'EPSG:3857');
var textent = ol.extent.applyTransform([6, 43, 16, 50], tfn);
var textent = ol.proj.transformExtent([6, 43, 16, 50], 'EPSG:4326', 'EPSG:3857');
到目前为止,我在http://ol3js.org/en/master/apidoc 中找不到 API 文档,因此您必须阅读 source 以获取信息。
API-Docs 自 BETA 以来已完成。所以你现在会找到它。
正如 cmets 中所述,正确的 API 函数现在是 ol.proj.transformExtent()。
【讨论】:
ol.proj.transformExtent。
在 OpenLayers 3.17.1 上,在尝试了各种方法后,我能够以两种不同的方式设置边界:
A) 作为@Grmpfhmbl mentioned,使用ol.proj.transformExtent 函数如下:
var extent = ol.proj.transformExtent(
[-0.6860987, 50.9395474, -0.2833177, 50.7948214],
"EPSG:4326", "EPSG:3857"
);
map.getView().fit( extent, map.getSize() );
B) 有点不寻常,像这样使用ol.geom.Polygon:
// EPSG:3857 is optional as it is the default value
var a = ol.proj.fromLonLat( [-0.6860987, 50.9395474], "EPSG:3857" ),
b = ol.proj.fromLonLat( [-0.2833177, 50.7948214], "EPSG:3857" ),
extent = new ol.geom.Polygon([[a, b]]);
map.getView().fit( extent, map.getSize() );
【讨论】: