本文介绍了如何将OpenLayers多边形坐标转换为纬度和经度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 OpenLayers 技术绘制多边形并保存该坐标.这是我的代码:-
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);
}
}
polygonString的坐标为
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?
推荐答案
您需要将几何图形从地图参考系统(EPSG:3857)转换为纬度和经度(EPSG:4326).
You need to transform the geometry from the map reference system (EPSG:3857) to latitude and longitude (EPSG:4326).
在drawend回调中尝试以下操作:
Try this in the drawend callback:
var geom = e.feature.getGeometry().transform('EPSG:3857', 'EPSG:4326');
console.log(geom.getCoordinates())
这篇关于如何将OpenLayers多边形坐标转换为纬度和经度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!