为什么openlayers在地图点击时返回投影坐标

Why openlayers is returning projected coordinates on map click?

本文关键字:返回 投影 坐标 openlayers 地图 为什么      更新时间:2023-09-26

我参与了一个使用ol3(v3.10)的地图项目我把地图的投影(我想)改成了"epsg 4258",它是ETRS89。

var customExtent = [-0.6172644,39.3868231,-0.43508204,39.44951748];
var etrs89 = new ol.proj.Projection({
      code: 'EPSG:4258',
      // 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: customExtent,
      units: 'degrees'
    });

现在我用这个投影设置我的地图。

var map = new ol.Map({
      controls: ol.control.defaults({
            attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
              collapsible: false,
              units: 'degrees'
            })
       }).extend([mousePositionControl,new ol.control.FullScreen()]),
       target: 'map',
       view: new ol.View({
            center: ol.proj.fromLonLat([-0.4685, 39.4315]),
            zoom: 14           
       }),
       layers: [
         ortofoto,
         muni_torrent
       ]
});

最后点击监听器看起来像:

var onclick = function(e){
    alert(e.coordinate);
};
map.on('click', onclick);

我得到的输出是:

[-53391.90122271185, 4785457.359197952]

但我期待的是:

[-0.4, 39.9]

我也尝试了以下相同的结果:

var onclick = function(e){
    var lonlat = map.getCoordinateFromPixel(e.pixel)
    alert('longitud: ' + lonlat[0]+ '; latitud: ' + lonlat[1]);
};

看起来点击事件不会输出地图投影中的坐标。一些想法?如有任何帮助,我们将不胜感激。

默认视图为EPSG:3857(球形mercator),因此这就是您所看到的坐标。

您需要将EPSG:4258指定为视图投影。请参阅:http://openlayers.org/en/v3.10.1/examples/sphere-mollweide.html?q=mollweide有关如何指定自定义视图投影的示例。

coordsDiv.textContent='lat:'+Math.round(event.latLng.lat())+','+'lng:'+Math.round(event.lateLng.lng());