【发布时间】:2016-08-16 07:01:18
【问题描述】:
我是OL的初学者,但我需要解决这个问题......
我想把这个数组放到我的地图上。谢谢帮忙! ;)
我的数组 ces 的片段:
["1192.4692,1107.0745","1190.5201,1107.0029","1190.5201,1101.8436","1190.5201,1098.0733", "1192.4162,1097.9464"]
var LineSource = new ol.source.Vector({
projection: 'EPSG:3857',
format: new ol.format.Feature(LineFeat)
});
var LineFeat = new ol.Feature( {
name: "krLine",
geometry: new ol.geom.MultiLineString(ces), });
LineSource.addFeature( LineFeat );
var krLineLayer = new ol.layer.Vector({
source: LineSource,
projection: 'EPSG:3857',
});
这是我的地图:
var map = new ol.Map({
layers: [
krLineLayer,
],
target: 'map',
controls: [
new ol.control.ScaleLine(),
new ol.control.MousePosition({
coordinateFormat: ol.coordinate.createStringXY(3),
})
],
view: mapView
});
这是@Mikelis 的一个解决方案和一个示例,我如何将线条放入我的地图:
var elem;
var arr2 = [];
for (var i=0; i<ces.length; i++){
elem = ces[i].split(",");
arr2.push([parseFloat(elem[0]), parseFloat(elem[1])]);
}
console.log(arr2);
var layerLines = new ol.layer.Vector({
source: new ol.source.Vector({
features: [new ol.Feature({
geometry: new ol.geom.LineString(arr2),
name: 'Line',
projection: 'EPSG:3857'
})]
}),
});
【问题讨论】:
-
我不知道你是否使用如图所示的数组,但
multilinestring将二维数组作为构造函数。每个数组元素是一个点的 x 和 y。像这样:new ol.geom.MultiLineString([ [[0, 0], [18, 60]], [[18, 60], [9, 30]] ]); -
是的,我知道这个事实,这就是我需要解决的问题。因为我的数组是由算法生成的。该算法生成具有数据作为坐标对的对象,如下所示:
data: "1190.5201,1107.0029"然后我将所有单独的 data 推入一个数组,现在我有了这个数组,我需要将其转换为正确的形式,但我有不知道怎么做。我尝试将.split(',')转换为单独的字符串,然后用括号"["+..+"["堆叠,但它不起作用。最后不管是作为LineString还是MultiLinestring放到地图中。我只需要可视化道路。
标签: arrays openlayers-3 multiline multilinestring