OpenLayers 2.x 中的OpenLayers.Bounds的概念是否仍然存在于 OpenLayers 3 中?它是如何变化的,它的新名称是什么?
4 回答
更新:OL4:https ://openlayers.org/en/latest/apidoc/ol.html#.Extent
似乎“边界”或“边界框”(BBOX)的新词是“范围”。看:
- 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
目前找出问题的一种方法是在 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());
只是为答案添加一个小例子:边界现在称为“范围”,它不再是复杂的对象/类,而只是一个由四个数字组成的数组。在“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-Doc,因此您必须阅读源代码才能获取信息。
API-Docs 自 BETA 以来已完成。所以你现在会找到它。
正如评论中提到的,正确的 API 函数现在是ol.proj.transformExtent()。
在 OpenLayers 3.17.1 上,在尝试了各种方法之后,我能够以两种不同的方式设置边界:
A)正如@Grmpfhmbl 提到的,使用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() );