【问题标题】:How do Bounds work in OpenLayers 3?Bounds 在 OpenLayers 3 中如何工作?
【发布时间】:2014-03-05 18:48:48
【问题描述】:

OpenLayers 2.x 中OpenLayers.Bounds 的概念是否仍然存在于 OpenLayers 3 中?它发生了怎样的变化,它的新名称是什么?

【问题讨论】:

  • var mapExtent = map.getView().calculateExtent(map.getSize());

标签: openlayers openlayers-3


【解决方案1】:

【讨论】:

  • 我可以为这些问题提供一些代表:),这个新标签将证明非常有用,因为 OL3 与 OL3 不兼容,目前两者都必须使用,否则没有人会看到这些问题。
  • 我的意思是“因为 OL3 与 OL2 不兼容”
【解决方案2】:

没有找到有关此功能的任何文档,但 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());

【讨论】:

【解决方案3】:

只是在答案中添加一个小例子: 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.extent.transform 已重命名为 ol.extent.applyTransform
  • 用于转换范围的函数是ol.proj.transformExtent
  • 谢谢。在撰写本文时,此功能不存在。 :-)
【解决方案4】:

在 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() );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多