0

I'm a beginer with OL, but I need to solve this problem... I want to get this array to my map . Thaks for help! ;) Segments of my array 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',
      });

And here is my map:

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

Here is a SOLUTION by @Mikelis and an Example, how I get Lines into my map:

    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'
          })]
      }),
  });
Jose Gómez
  • 3,110
  • 2
  • 32
  • 54
  • I don't know if you are using the array as it is shown, but `multilinestring` takes 2d array as a constructor. Each array element is x and y of a point. Sth like this: `new ol.geom.MultiLineString([ [[0, 0], [18, 60]], [[18, 60], [9, 30]] ]);` – Mike B Apr 22 '16 at 21:00
  • yes i know that fact, and this is what I need to solve.. because my array is generated by an algorithm. this algorithm generate objects with data as coordinate pairs like this: `data: "1190.5201,1107.0029"` then I pushed all individual **data** into one array and now I have this array which I need to convert to the correct form, but i have no idea how. I've tried to `.split(',')` it into indivitual strings and then stack with brackets `"["+..+"["` but it does not work. finally it doesn't matter that it will be put into map as LineString or MultiLinestring.I just need to visualize the road. – Csaba Seres Apr 23 '16 at 00:44

1 Answers1

0

You already have an array of pairs. Take each element of that array, split in two with split(","). This gives you an array[2] of strings. Then convert each of the strings to number and stick into new array also as array[2]. Tested.

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])]);
}
Mike B
  • 2,756
  • 2
  • 16
  • 28