Skip to content

Commit

Permalink
Added support for showing vehicle-positions in the map (#174)
Browse files Browse the repository at this point in the history
* Added support for showing vehicle-positions in the map

* Update doc
  • Loading branch information
lassetyr authored Apr 22, 2024
1 parent 31f0e6f commit 0d47aa4
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/components/Map/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState, useEffect, useMemo } from 'react';
import { MapContainer, TileLayer, GeoJSON, useMap } from 'react-leaflet';
import Leaflet from 'leaflet';
import bbox from '@turf/bbox';
import { lineString, featureCollection } from '@turf/helpers';
import { lineString, featureCollection, point } from '@turf/helpers';
import lineToPolygon from '@turf/line-to-polygon';
import { toGeoJSON } from '@mapbox/polyline';
import { colors } from '@entur/tokens';
Expand Down Expand Up @@ -85,15 +85,40 @@ function getMapData(responseData) {
legLines: getLegLines(responseData),
flexibleAreas: getFlexibleAreas(responseData),
serviceJourney: getServiceJourneyLines(responseData),
vehiclePositions: getVehiclePositions(responseData),
};
}

// Returns an array of points
function getVehiclePositions(responseData) {
if (!responseData) return;

const vehicles = responseData.data?.vehicles;

if (!vehicles) {
return [];
}

const vehiclePositions = vehicles
.map((vehicle) => vehicle?.location)
.filter(Boolean)
.map((location) => point([location.longitude, location.latitude]));

return vehiclePositions;
}

function MapContent({ mapData }) {
const map = useMap();

const collection = useMemo(() => {
const { legLines, flexibleAreas, serviceJourney } = mapData;
const allFeatures = [...legLines, ...flexibleAreas, ...serviceJourney];
const { legLines, flexibleAreas, serviceJourney, vehiclePositions } =
mapData;
const allFeatures = [
...legLines,
...flexibleAreas,
...serviceJourney,
...vehiclePositions,
];
return allFeatures.length > 0 ? featureCollection(allFeatures) : null;
}, [mapData]);

Expand Down

0 comments on commit 0d47aa4

Please sign in to comment.