简体   繁体   English

使用一个点 wkt 作为一个带有 openlayers 的圆

[英]Use a point wkt as a circle with openlayers

I am looking for a way to draw circles within the WKT format using Openlayers.我正在寻找一种使用 Openlayers 在 WKT 格式中绘制圆圈的方法。 I know that the WKT standard doesn't support circles but someone said that you might be able to use a point wkt and then set a radius for it (for android but might work for other things as well).我知道 WKT 标准不支持圆,但有人说你可以使用一个点 wkt 然后为它设置一个半径(对于 android 但也可能适用于其他事情)。 my question is thus how do I do that in Openlayers if it is possible?因此,我的问题是,如果可能的话,我该如何在 Openlayers 中做到这一点?

link to what Tom said https://stackoverflow.com/a/58430532链接到汤姆所说的 https://stackoverflow.com/a/58430532

This is how I made a polygon这就是我制作多边形的方式

     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);

I'm asking this question to see if I can make the drawing/saving of the coordinates simpler.我在问这个问题,看看我是否可以使坐标的绘制/保存更简单。 Right now, I have the coordinates in a string “coord1, coord2;”现在,我在字符串“coord1, coord2;”中有坐标。 and have to split them when using a polygon and then convert them back into that string format when saving the coordinates.并且在使用多边形时必须将它们拆分,然后在保存坐标时将它们转换回该字符串格式。 With wkt I can just throw the string into the Openlayers function and I'm done so if I can kind of do the same with the circle then that would be great.使用 wkt,我可以将字符串放入 Openlayers function 并且我已经完成了,如果我可以对圆圈做同样的事情,那就太好了。

What I use Openlayers v6 (some version of that) vanilla js, php我使用什么 Openlayers v6(一些版本)vanilla js,php

If you always wrap the wkt in a JSON如果您总是将 wkt 包装在 JSON

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

You could optionally add a radius to indicate a circle您可以选择添加半径来指示圆

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

Then OpenLayers could parse the JSON string and convert a point with a radis to a circle然后 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM