-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
5 Show Routes' Directions.svelte
82 lines (69 loc) · 2.43 KB
/
5 Show Routes' Directions.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<script lang="ts">
import { onMount } from "svelte";
import { examples } from "../router";
import { location } from "svelte-spa-router";
import AppSidebar from "../components/AppSidebar.svelte";
import maplibregl from "maplibre-gl";
import "maplibre-gl/dist/maplibre-gl.css";
import style from "../assets/map/style/style.json?url";
import MapLibreGlDirections, { layersFactory } from "@maplibre/maplibre-gl-directions";
import DirectionArrowImageUrl from "../assets/map/images/direction-arrow.png?url";
const meta = examples.find((example) => example.path === $location)!;
let mapRef: HTMLElement;
let directions: MapLibreGlDirections;
onMount(() => {
const map = new maplibregl.Map({
container: mapRef,
style,
center: [-74.1197632, 40.6974034],
zoom: 11,
attributionControl: false,
});
map.addControl(
new maplibregl.AttributionControl({
customAttribution: "<a href='http://project-osrm.org/' target='_blank'>© OSRM</a>",
}),
);
// load the arrow image and add it to the map
map.loadImage(DirectionArrowImageUrl).then((image) => {
if (image) {
map.addImage("direction-arrow", image.data);
}
});
const layers = layersFactory();
// add a direction arrow layer
layers.push({
id: "maplibre-gl-directions-routeline-direction-arrow",
type: "symbol",
source: "maplibre-gl-directions",
layout: {
"symbol-placement": "line-center",
"icon-image": "direction-arrow",
"icon-size": ["interpolate", ["exponential", 1.5], ["zoom"], 12, 0.85, 18, 1.4],
},
paint: {
"icon-opacity": 0.5,
},
filter: ["==", ["get", "route"], "SELECTED"],
});
map.on("load", () => {
directions = new MapLibreGlDirections(map, {
requestOptions: {
alternatives: "true",
},
layers,
});
directions.interactive = true;
});
});
</script>
<AppSidebar>
<span slot="title">{meta.name}</span>
<p>Another example that demonstrates the ease of extending the original styles provided by the plugin.</p>
<p>This time a "symbol" layer is added that shows the direction the selected route goes in.</p>
<small
><strong>Note</strong> that you have to manually load and add the images you intend to use for the custom layers you
add</small
>
</AppSidebar>
<div bind:this={mapRef} class="basis-full lg:basis-2/3 shadow-xl" />