如何在 Openlayers 3 中获取用户绘制的圆的坐标?

How to get coordinates of a user-drawn circle in Openlayers 3?

我在 Openlayers3 中有一个静态图像层,用户可以在其中绘制圆、点和多边形等特征。

我可以用这段代码获取点、多边形和线串的坐标:

draw_interaction.on('drawend', function(event) {
    var geoJsonGeom = new ol.format.GeoJSON();    
    var pp = geoJsonGeom.writeGeometry(event.feature.getGeometry());
    console.log(pp);
});

这会按预期输出坐标:

"{"type":"Point","coordinates":[1441.9187662124637,1365.3125032424925]}"

但是,我不知道如何获取圆的坐标。 Circle 情况下的输出如下所示:

"{"type":"GeometryCollection","geometries":[]}"

Openlayers 版本为 v3.5.0。

编辑: 为了获得中心和半径,你必须使用特征本身,而不是它的 GeoJSON 输出(因为 GeojSON 没有圆):

var circle = event.feature.getGeometry();
console.log('radius:' + circle.getRadius());
console.log('center:' + circle.getCenter());

GeoJSON 没有圆形几何图形。正确的解决方案是在序列化之前用多边形近似圆。您可以使用 ol.interaction.Draw 的新几何选项来实现此目的,请参阅:https://github.com/openlayers/ol3/pull/3673 另请参阅此 github 问题以获取有关该主题的更深入讨论:https://github.com/openlayers/ol3/pull/3434

draw_interaction.on('drawend', function(event) {
    var geoJsonGeom = new ol.format.GeoJSON();    
    var pp = geoJsonGeom.writeGeometry(event.feature.getGeometry());
    console.log(pp);
});

效果不错

对于所有正在寻找从圆中获取坐标的解决方案的开发人员:

class“多边形”有一个名为“fromCircle”的方法。从那里你可以使用 .getCoordinates()

https://openlayers.org/en/latest/apidoc/module-ol_geom_Polygon.html#.fromCircle

它也可用 int v3.6.0 http://www.scgis.net/api/ol/v3.6.0/apidoc/ol.geom.Polygon.html