如何在Openlayers 3.5中的不同投影中添加功能

时间:2015-06-07 06:55:42

标签: openlayers-3

我正在使用Openlayer 3.5并加载OSM地图" EPSG:3857"。

var extent = [116.826673, 4.854776, 126.748593, 18.697146];
var philiExtent = ol.extent.applyTransform(extent, ol.proj.getTransform("EPSG:4326", "EPSG:3857"));

var view = new ol.View({
    center: ol.proj.transform([121.787633, 11.775961], 'EPSG:4326', 'EPSG:3857'),
    zoom: 0,
    extent: philiExtent,
    resolutions: [2560, 1280, 640, 320, 160, 80, 40, 20, 10, 5, 2.5, 1.2, 0.6],
});

var map = new ol.Map({
    layers: [
      new ol.layer.Tile({
          source: new ol.source.OSM()
      })
    ],
    target: 'map'
});

但我的webService功能在" EPSG:4326"

function showData(data) {
    var format = new ol.format.WKT();
    var feature;
    $.each(data, function (i, link) {
        feature = format.readFeature(link.geom);
        wktTraffic.addFeature(feature);
    })
    console.log('done load map');
}

所以我如何使地图在4326上,或新功能在3857上。 我更喜欢第一种选择。

2 个答案:

答案 0 :(得分:2)

查看常见问题解答部分:http://openlayers.org/en/master/doc/faq.html#how-do-i-change-the-projection-of-my-map-

如何更改地图的投影? 您很可能希望将OpenLayers的默认预测更改为更适合您所在地区或特定数据的内容。

可以通过view-property设置地图的投影。以下是一些例子:

// OpenLayers支持1984年世界大地测量系统,EPSG:4326:

var map = new ol.Map({
  view: new ol.View({
    projection: 'EPSG:4326'
    // other view properties like map center etc.
  })
  // other properties for your map like layers etc.
});

// To use other projections, you have to register the projection in OpenLayers:
//
// By default OpenLayers does not know about the EPSG:21781 (Swiss) projection.
// So we create a projection instance for EPSG:21781 and pass it to
// ol.proj.addProjection to make it available to the library for lookup by its
// code.
var swissProjection = new ol.proj.Projection({
  code: 'EPSG:21781',
  // The extent is used to determine zoom level 0. Recommended values for a
  // projection's validity extent can be found at http://epsg.io/.
  extent: [485869.5728, 76443.1884, 837076.5648, 299941.7864],
  units: 'm'
});
ol.proj.addProjection(swissProjection);

// we can now use the projection:
var map = new ol.Map({
  view: new ol.View({
    projection: swissProjection
    // other view properties like map center etc.
  })
  // other properties for your map like layers etc.
});

我们建议您在epsg.io上查找投影的参数(如有效范围)。

答案 1 :(得分:2)

要将您的功能重新投放到EPSG:3857,您可以在解析WKT字符串中的功能时设置选项dataProjectionfeatureProjection。另请参阅ol.format.WKT#readFeature

var format = new ol.format.WKT();
var feature;
$.each(data, function (i, link) {
    feature = format.readFeature(link.geom, {
      dataProjection: 'EPSG:4326',
      featureProjection: 'EPSG:3857'
    });
    wktTraffic.addFeature(feature);
})