簡體   English   中英

如何將OpenLayers多邊形坐標轉換為緯度和經度?

[英]How to convert OpenLayers polygon coordinates into latitude and longitude?

我使用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);
    }
}

polygonString的坐標形式為

[-12086017.297876,6615491.5618235]
[-12095801.237496,6615491.5618235]

我想將這些值保存為緯度和經度。 我怎樣才能做到這一點?

您需要將幾何圖形從地圖參考系統(EPSG:3857)轉換為緯度和經度(EPSG:4326)。

在drawend回調中嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM