This response on image below is coming from Geoserver as MultiLineString and I restructured it to LineString like on image because Animation plugin doesn`t support MultiPolyline json structure and because response has coordinates as lng, lat and I need lat, lng as Leaflet defines. This structure on image works nice with animation plugin.
Since Leaflet Animated Marker Plugin I use doesn't support MultiLineStrings I had to structure it as LineString. It accepts this LineString structure but coordinates are mixed after I apply coordsToLatLngs static method. That makes my marker not appearing on first position and jumping back and forth. Until this line of code coordinates are in right order, as defined in SQL View on Geoserver and I could check it and prove in DB and in QGIS
This is view on Geoserver:
SELECT pgr.seq AS seq,
pgr.edge AS edge,
e.id AS id,
SUM(e.distance) AS distance,
st_linemerge(st_union(e.geom)) AS geom
FROM pgr_dijkstra('SELECT id, source, target,onewayNum as cost, r_cost as
reverse_cost FROM parking.n_streets_noded',%source%,%target%, true) AS
pgr, parking.n_streets_noded AS e
WHERE pgr.edge = e.id
GROUP BY pgr.seq, e.geom,e.name,e.type, pgr.seq, pgr.edge,e.id
ORDER by pgr.seq
This is function with ajax call:
function getFinalRoute(){
var urlRoute = `${geoserver}/wfs?service=WFS&version=1.0.0&request=GetFeature&
typeName=xxx:shortestpath&viewparams=source:${source};target:${targetN || targetE}&outputformat=application/json
&srsName=EPSG:4326`;
var routeLayer = L.geoJSON(null);
var flippedCoords;
$.ajax({
url: urlRoute,
async: false,
success: function(response){
console.log(response)
var routeArr = response.features;
var coordsArr = Object.keys(routeArr).map(key => {
return routeArr[key]
})
var xxy = coordsArr.map(function(feature){
var obj = feature.geometry.coordinates[0];
return Object.keys(obj).map(function(key){
return obj[key];
})
})
//code above gives me coordinates in perfect order but after applying this method below their order mixes up
var flipCoor = L.GeoJSON.coordsToLatLngs(xxy,1);
var flippedCoor = flipCoor.sort().map(function(obj){
return Object.values(obj).sort().map(values => {
return Object.values(values)
})
})
var myArray = [].sort().concat(...flippedCoor);
console.log(myArray);
var multiLineString = L.polyline(myArray);
var secondAnimated = L.animatedMarker(multiLineString.getLatLngs(), {
distance: 100,
interval: 500,
iconSize:[16,16],
iconAnchor: [7, 16],
//autostart: false,
icon: pulsingIcon
});
map.addLayer(secondAnimated);
routeLayer = L.geoJSON(response)
}
})
map.addLayer(routeLayer);
};
Leaflet.AnimatedMarker
plugin does not know how to work with MultiPolylines (tested). You have to animate each segment separately.onEnd
option to start animation of next segment when animation of the current one ends.