【问题标题】:Drawn polygon does not show in openlayers绘制的多边形不显示在 openlayers
【发布时间】:2019-05-20 21:40:10
【问题描述】:

我必须在 openlayers 地图上绘制一个多边形。这是我的代码:

   draw = new Draw({
        source: this.vectorSource,
        type: 'Polygon'
    })
    draw.on('drawend', e => {

             // sol 1, result is not as required
             let coords = e.feature.getGeometry().getCoordinates()

             //sol 2, give correct results, but drawn polygon gone
             let coords = e..feature.getGeometry().transform('EPSG:3857', 'EPSG:4326').getCoordinates()
    }
    this.olmap.addInteraction(draw)

我必须将转换后的坐标存储在 DB 中,但解决方案 #2 不能保持绘制多边形的可见性。 在解决方案 #1 的情况下,如果我稍后尝试使用

转换它们,它不会提供所需的格式化坐标
    transform(coords, 'EPSG:3857', 'EPSG:4326')

它不返回格式化的坐标。 请指导我在哪里出错以保持多边形的可见性并获取转换后的坐标。

【问题讨论】:

    标签: javascript vue.js openlayers openlayers-5


    【解决方案1】:

    你需要克隆几何体

    let coords = e..feature.getGeometry().clone().transform('EPSG:3857', 'EPSG:4326').getCoordinates();
    

    否则,您会将要素移动到视图坐标中靠近点 [0, 0] 的位置

    【讨论】:

    • 谢谢,它可以根据需要正常工作。我将阅读 clone() 函数的详细信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多