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

时间:2015-05-28 10:43:50

标签: openlayers-3

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

我可以使用以下代码获取Point,Polygon和LineString的坐标:

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的坐标。 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());

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

对于所有寻求解决方案以将坐标从圆内移出的开发人员:

“多边形”类有一个名为“ 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

答案 2 :(得分:-1)

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

效果很好