2014-03-05 46 views

回答

8
+1

我可以给你一些代表这些问题:),那个新标签将被证明是相当有用的,因为OL3与OL3不兼容,因为现在必须使用两者,否则没有人会看到这些问题。 –

+0

我的意思是'因为OL3与OL2不兼容' –

5

只是为答案增加一个小例子: 边界现在被称为“范围”,它不再是一个复杂的对象/类,而只是四个数字的数组。 “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'); 

我无法找到一个API的文档中 http://ol3js.org/en/master/apidoc到目前为止您已经阅读:刚上如何来转换一个小例子 source获取信息。

API-Docs自BETA以来已经完成。所以你现在就可以找到它。

正如评论中所述,正确的API函数现在是ol.proj.transformExtent()

+0

我认为ol.extent.transform已被重命名为ol.extent.applyTransform – Peter

+0

谢谢。我更新了这个例子。 – Grmpfhmbl

+0

用于转换范围的函数是'ol.proj.transformExtent'。 – erilem

5

Did'nt发现有关此功能的任何文档,但程度似乎工作:

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()还可以通过任何程度对象来代替,就像这样:

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

由于OpenLayer 3.9,该方法改变了:

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

+2

谢谢。这似乎是目前文档的**程度**:http://openlayers.org/en/v3.0.0/apidoc/ol.extent.html – colllin

1

论的OpenLayers 3.17。1和尝试不同的东西,我能够设定的范围有两种不同的方式后:

A)作为@Grmpfhmblmentioned,使用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());