首页 > 解决方案 > 使用一个点 wkt 作为一个带有 openlayers 的圆

问题描述

我正在寻找一种使用 Openlayers 在 WKT 格式中绘制圆圈的方法。我知道 WKT 标准不支持圆,但有人说你可以使用一个点 wkt 然后为它设置一个半径(对于 android,但也可能适用于其他东西)。因此,我的问题是,如果可能的话,我该如何在 Openlayers 中做到这一点?

链接到汤姆所说的https://stackoverflow.com/a/58430532

这就是我制作多边形的方式

     let polyFeature = new ol.format.WKT().readFeature(polygonWKT, {
    // Must use both projections in order to draw the feature with the wkt format
    dataProjection   : "EPSG:4326",
    featureProjection: map.getView().getProjection() // Can at least get the standard projection and not have to fiddle with that
  });
  vectorSource.addFeature(polyFeature);

我在问这个问题,看看我是否可以使坐标的绘制/保存更简单。现在,我在字符串“coord1, coord2;”中有坐标。并且在使用多边形时必须将它们拆分,然后在保存坐标时将它们转换回该字符串格式。使用 wkt,我可以将字符串放入 Openlayers 函数中,然后我就完成了,如果我可以对圆圈做同样的事情,那就太好了。

我使用什么 Openlayers v6(一些版本)vanilla js, php

标签: javascriptgeometryopenlayersopenlayers-6wkt

解决方案


如果您总是将 wkt 包装在 JSON 中

'{"wkt":"POINT(28.625360369528934 77.2227479486792)"}'

您可以选择添加半径来指示圆

'{"wkt":"POINT(28.625360369528934 77.2227479486792)","radius":50}'

然后 OpenLayers 可以解析 JSON 字符串并将带半径的点转换为圆

let json = JSON.parse(jsonString);
let polyFeature = new ol.format.WKT().readFeature(json.wkt, {
    dataProjection   : "EPSG:4326",
    featureProjection: map.getView().getProjection()
});
if (json.radius && polyFeature.getGeometry().getType() === 'Point') {
    polyFeature.setGeometry(new ol.geom.Circle(polyFeature.getGeometry().getCoordinates(), json.radius));
}
vectorSource.addFeature(polyFeature);

推荐阅读