我希望将我的应用程序从 OpenLayers 2 更新到 OpenLayers 3。
有没有人知道可以对此有所帮助的迁移指南(或类似的东西)?
FWIW - 当我们将http://www.nufosmatic.com上的简单页面从 ol2迁移到 ol3 时,我们愿意做出贡献。问题看起来很棘手,但很多原因是 ol3 看起来比 ol2 好得多,示例看起来也有了很大改进,文档也好多了,但它们不同,如果你有终于习惯了ol2 docs。
命名空间已经改变,一些 API 调用的顺序必须由于一些语义差异而改变。这是一个头脑简单的一阶地图迁移。这个简单的练习花了大约一个小时,主要是由于上面提到的新文档混乱:
/*
Very simple OpenLayers2 map
*/
var map, layer, center;
function init() {
map = new OpenLayers.Map('map');
layer = new OpenLayers.Layer.OSM("Simple OSM Map");
map.addLayer(layer); // this must come before the following
center = new OpenLayers.LonLat(-71.147, 42.472)
.transform(
new OpenLayers.Projection("EPSG:4326"),
map.getProjectionObject()
);
map.setCenter(center, 5);
}
/*
Very simple OpenLayers3 map
*/
var map, layer, center;
function init(){
map = new ol.Map({
target:'map',
renderer:'canvas',
view: new ol.View({
projection: 'EPSG:900913',
})
});
layer = new ol.layer.Tile({
source: new ol.source.OSM()
});
map.addLayer(layer); // this can actually come up last
center = new ol.proj.transform([-71.147, 42.472],
'EPSG:4326', // essentially LonLat
map.getView().getProjection());
map.getView().setCenter(center);
map.getView().setZoom(5);
}
顶层 html 在标记中发生了一些变化,其中包含一些包装器更改(以上位于 js/main.js 文件中):
> diff -btw ../*[23]/index.html
7c7
< <script src='OpenLayers-2.13.1/OpenLayers.js'></script>
---
> <script src='v3.10.1/build/ol.js'></script>
11c11
< <link rel='stylesheet' href='OpenLayers-2.13.1/theme/default/style.css'>
---
> <link rel='stylesheet' href='v3.10.1/css/ol.css'>
OpenLayers3 has a new design and concepts and modelled different, so there is no direct translation.
I think the best option is starting reading the current available books so you can learn it and evaluate yourself:
Take into account OL3 has many improvements over OL2 but not all the features of OL2 are implemented in OL3 yet.