最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to convert OpenLayers polygon coordinates into latitude and longitude? - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Nov 17, 2017 at 12:56 SaswatSaswat 12.9k18 gold badges84 silver badges160 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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())

In openlayers 10. You can use this code to transform latlong to coordinate and coordinate to latlong:

        import {fromLonLat,toLonLat} from 'ol/proj.js';
        
        .
        .
        .
        
        map.on('click', function (evt) {
          let coordinate=evt.coordinate;
          let latlong=toLonLat(coordinate);
          let coordinate2=fromLonLat(latlong);
 
   console.log("coordinate:"+coordinate, "latlong:"+latlong, "coordinate2:"+coordinate2);
        });
发布评论

评论列表(0)

  1. 暂无评论