i am trying to draw a lineString using the same coordinates that are used to draw a collection of markers on a map. The markers are drawn, then the lineString should draw joining the markers, the lineString uses the same coords as the markers.
I am having strange issues though, sometimes all the lines draw, sometimes only one of the lines draw. but usually there will be one or two lines missing. when i run getCoordinates() on the lineString, it returns me all the same coordinates as the marker locations, yet some of the lines are not drawn.
some code:
// location data, contains x/y coords
var locs = JSON.parse(loc_data);
var lineCoords = [];
var lat;
var lng;
// loop through loc data and output the markers
for (var key in locs) {
if (locs.hasOwnProperty(key)) {
lat = locs[key].lat;
lng = locs[key].lng;
// store the coords in an array to be used for the lineString
lineCoords.push([lat,lng]);
// create marker and add to map
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point([lat, lng]),
});
var vectorSource = new ol.source.Vector({
features: [iconFeature]
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
map.addLayer(vectorLayer);
}
}
// draw lineString using coordinates in lineCoords array
var line = new ol.geom.LineString(lineCoords);
var layerLines = new ol.layer.Vector({
source: new ol.source.Vector({
features: [new ol.Feature({
geometry: line,
name: 'Line'
})]
}),
});
map.addLayer(layerLines);
the code above seems pretty logical, i can't see where there could be a problem with it, and like is said, sometimes all lines are drawn and sometimes only one is drawn.
can anyone shine some light on this?