How to convert OpenLayers polygon coordinates into

2019-08-10 22:35发布

I am using the OpenLayers technology to draw polygon and save the coordinates. Here is my code:-

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);
    }
}

The polygonString has coordinates in the form of

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

I want to save these values as latitude and longitude. How can I do this?

1条回答
神经病院院长
2楼-- · 2019-08-10 23:04

You need to transform the geometry from the map reference system (EPSG:3857) to latitude and longitude (EPSG:4326).

Try this in the drawend callback:

var geom = e.feature.getGeometry().transform('EPSG:3857', 'EPSG:4326');
console.log(geom.getCoordinates())
查看更多
登录 后发表回答