获取数组数据(线段)以映射多线 openlayers 3

get array data (linesegments) to map multiLine openlayers 3

我是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'
          })]
      }),
  });

您已经有了一组对。取出该数组的每个元素,用 split(",") 一分为二。这为您提供了 array[2] 个字符串。然后将每个字符串转换为数字并作为 array[2] 粘贴到新数组中。已测试。

var arr = ["1192.4692,1107.0745","1190.5201,1107.0029","1190.5201,1101.8436","1190.5201,1098.0733", "1192.4162,1097.9464"];
var elem;
var arr2 = [];
for (var i=0; i<arr.length; i++){
   elem = arr[i].split(",");
   arr2.push([parseFloat(elem[0]), parseFloat(elem[1])]);
}