如何将 OpenLayers 多边形坐标转换为经纬度?

Sas*_*wat 2 javascript openlayers

我正在使用OpenLayers技术绘制多边形并保存坐标。这是我的代码:-

var raster = new ol.layer.Tile({
                source: new ol.source.OSM()
            });

var source = new ol.source.Vector({wrapX: false});

var vector = new ol.layer.Vector({
                source: source
            });

var map = new ol.Map({
            layers: [raster, vector],
            target: 'map',
            view: new ol.View({
                center: [-11000000, 4600000],
                zoom: 4
            })
        });

var typeSelect = document.getElementById('type');

var draw; // global so we can remove it later
function addInteraction() {
    var value = typeSelect.value;
    if (value !== 'None') {
        draw = new ol.interaction.Draw({
                source: source,
                type: /** @type {ol.geom.GeometryType} */ (typeSelect.value),
                freehand: true
            });
        draw.on('drawend',function(e){
            var polygonString = e.feature.getGeometry().getCoordinates();
            //polygonString = polygonString.toString();
            //$('#polygonString').val(polygonString);
            //$('#myPlot').modal('show');
        }); 
        map.addInteraction(draw);
    }
}
Run Code Online (Sandbox Code Playgroud)

多边形字符串的坐标形式为

[-12086017.297876,6615491.5618235]
[-12095801.237496,6615491.5618235]
Run Code Online (Sandbox Code Playgroud)

我想将这些值保存为纬度和经度。我怎样才能做到这一点?

小智 5

您需要将几何图形从地图参考系统 (EPSG:3857) 转换为纬度和经度 (EPSG:4326)。

在 drawend 回调中试试这个:

var geom = e.feature.getGeometry().transform('EPSG:3857', 'EPSG:4326');
console.log(geom.getCoordinates())
Run Code Online (Sandbox Code Playgroud)