【问题标题】:Draw a polygon using openLayers使用 openLayers 绘制多边形
【发布时间】:2016-08-08 07:02:35
【问题描述】:

我有一组坐标,我想用它们用 OpenLayers 绘制一个多边形。坐标如下:

[["50.12345","30.12345"],["40.12345","20.12345"],["60.12345","10.12345"],["70.12345","90.12345"]]

如何使用这些坐标绘制多边形?我正在尝试以下方法,但它似乎不起作用:

var coords = "[["50.12345","30.12345"],["40.12345","20.12345"],["60.12345","10.12345"],["70.12345","90.12345"]]";
var polygon = new ol.geom.Polygon([coords]);

polygon.transform('ESPG:4326','ESPG:3857');
var feature = new ol.feature(polygon);
var vectorSource = new ol.source.Vector({});
vectorSource.addFeature(feature);
layer = new ol.layer.Vector({
source: vectorSource});

map.addLayer(layer);

有什么想法吗?谢谢!

【问题讨论】:

  • 对于初学者,使用 parseFloat 将字符串转换为浮点数

标签: javascript polygon openlayers openlayers-3


【解决方案1】:
// instead of this - a string
var coords = "[["50.12345","30.12345"],["40.12345","20.12345"],["60.12345","10.12345"],["70.12345","90.12345"]]";

// change to an array of arrays - remove the beginning quotes
var coords = [["50.12345","30.12345"],["40.12345","20.12345"],["60.12345","10.12345"],["70.12345","90.12345"]];

// and then you have to convert these string coordinates to number type
coords.map(function(coord){
  return [parseFloat(coord[0]), parseFloat(coord[1])];
});

继续其余部分 - 请注意ol.Feature 用大写字母书写。

【讨论】:

    猜你喜欢
    • 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
    相关资源
    最近更新 更多