Skip to content

Commit

Permalink
Merge pull request #3 from derGraph/show-tracks-on-map
Browse files Browse the repository at this point in the history
Show tracks on map
  • Loading branch information
derGraph authored Aug 20, 2024
2 parents 37c91bb + 0e481cb commit d1b18e1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
26 changes: 25 additions & 1 deletion src/lib/Leaflet.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
<script lang="ts">
import L from 'leaflet';
import 'leaflet/dist/leaflet.css';
import { onDestroy, onMount, setContext } from 'svelte';
import { modeCurrent } from '@skeletonlabs/skeleton';
let map: L.Map | undefined;
let mapElement: HTMLDivElement;
interface Datapoint {
id?: string;
time?: Date;
tripId: string;
lat: number;
long: number;
speed?: number;
heading?: number;
depth?: number;
h_accuracy?: number;
v_accuracy?: number;
propulsion?: number;
}
onMount(() => {
map = L.map(mapElement);
Expand All @@ -21,7 +36,7 @@
});
osmLayer.addTo(map);
seamarkLayer.addTo(map);
//seamarkLayer.addTo(map);
});
onDestroy(() => {
Expand All @@ -36,6 +51,7 @@
export let bounds: L.LatLngBoundsExpression | undefined = undefined;
export let view: L.LatLngExpression | undefined = undefined;
export let zoom: number | undefined = undefined;
export let tracks: Datapoint[] | null = null;
onMount(() => {
if (!bounds && (!view || !zoom)) {
Expand All @@ -49,6 +65,14 @@
} else if (view && zoom) {
map.setView(view, zoom);
}
let latlngs: L.LatLng[] = [];
if(tracks != null){
for(let point of tracks){
latlngs.push(new L.LatLng(point.lat, point.long))
}
let polyline = L.polyline(latlngs, {color: 'red'}).addTo(map);
}
}
</script>

Expand Down
22 changes: 21 additions & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,30 @@
// @ts-ignore
import type { LatLngExpression } from 'leaflet';
import Leaflet from '$lib/Leaflet.svelte';
import { onMount } from 'svelte';
import errorStore from '$lib/errorStore.js';
export let data;
let tracks: null = null;
const initialView: LatLngExpression = [43.95, 14.79];
async function getTrip(tripId: String) {
let response = await fetch('/api/Datapoints?tripId='+tripId);
if(!response.ok){
$errorStore = response;
}
tracks = await response.json();
}
onMount(()=>{
if(data.user){
getTrip(data.user?.activeTripId);
}
});
</script>

<div class="md:container md:mx-auto py-3 h-full rounded">
<Leaflet view={initialView} zoom={8} />
<Leaflet view={initialView} zoom={8} tracks={tracks}/>
</div>

0 comments on commit d1b18e1

Please sign in to comment.