Files
gpx.studio/website/src/lib/components/Map.svelte

153 lines
3.0 KiB
Svelte
Raw Normal View History

2024-04-05 17:53:42 +02:00
<script lang="ts">
2024-04-17 11:44:37 +02:00
import { onMount } from 'svelte';
2024-04-05 17:53:42 +02:00
2024-04-10 14:54:35 +02:00
import mapboxgl from 'mapbox-gl';
import 'mapbox-gl/dist/mapbox-gl.css';
2024-04-05 17:53:42 +02:00
2024-04-10 14:54:35 +02:00
import MapboxGeocoder from '@mapbox/mapbox-gl-geocoder';
import '@mapbox/mapbox-gl-geocoder/dist/mapbox-gl-geocoder.css';
2024-04-05 17:53:42 +02:00
2024-04-17 11:44:37 +02:00
import { map } from '$lib/stores';
2024-04-10 14:54:35 +02:00
mapboxgl.accessToken =
'pk.eyJ1IjoiZ3B4c3R1ZGlvIiwiYSI6ImNrdHVoM2pjNTBodmUycG1yZTNwcnJ3MzkifQ.YZnNs9s9oCQPzoXAWs_SLg';
export let distanceUnits: 'metric' | 'imperial' = 'metric';
2024-04-22 10:45:02 +02:00
let fitBoundsOptions: mapboxgl.FitBoundsOptions = {
maxZoom: 15,
linear: true,
easing: () => 1
};
2024-04-05 17:53:42 +02:00
onMount(() => {
2024-04-17 11:44:37 +02:00
$map = new mapboxgl.Map({
2024-04-05 17:53:42 +02:00
container: 'map',
2024-04-11 17:18:21 +02:00
style: { version: 8, sources: {}, layers: [] },
2024-04-12 12:38:19 +02:00
projection: { name: 'mercator' },
2024-04-10 14:54:35 +02:00
hash: true,
language: 'auto',
attributionControl: false,
2024-04-20 18:47:16 +02:00
logoPosition: 'bottom-right',
boxZoom: false
2024-04-05 17:53:42 +02:00
});
2024-04-17 11:44:37 +02:00
$map.addControl(
2024-04-10 14:54:35 +02:00
new mapboxgl.AttributionControl({
compact: true
2024-04-05 17:53:42 +02:00
})
);
2024-04-17 11:44:37 +02:00
$map.addControl(new mapboxgl.NavigationControl());
2024-04-10 14:54:35 +02:00
2024-04-17 11:44:37 +02:00
$map.addControl(
2024-04-10 14:54:35 +02:00
new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
mapboxgl: mapboxgl,
2024-04-22 10:45:02 +02:00
collapsed: true,
flyTo: fitBoundsOptions
2024-04-10 14:54:35 +02:00
})
);
2024-04-17 11:44:37 +02:00
$map.addControl(
2024-04-10 14:54:35 +02:00
new mapboxgl.GeolocateControl({
2024-04-05 17:53:42 +02:00
positionOptions: {
enableHighAccuracy: true
},
2024-04-22 10:45:02 +02:00
fitBoundsOptions,
2024-04-10 14:54:35 +02:00
trackUserLocation: true,
showUserHeading: true
2024-04-05 17:53:42 +02:00
})
);
2024-04-17 11:44:37 +02:00
$map.addControl(
2024-04-10 14:54:35 +02:00
new mapboxgl.ScaleControl({
unit: distanceUnits
2024-04-05 17:53:42 +02:00
})
);
2024-04-19 17:26:56 +02:00
$map.on('style.load', () => {
if ($map) {
if (!$map.getLayer('mapbox-dem')) {
$map.addSource('mapbox-dem', {
type: 'raster-dem',
url: 'mapbox://mapbox.mapbox-terrain-dem-v1',
tileSize: 512,
maxzoom: 14
});
}
$map.setTerrain({ source: 'mapbox-dem', exaggeration: 1 });
$map.setFog({
color: 'rgb(186, 210, 235)',
'high-color': 'rgb(36, 92, 223)',
'horizon-blend': 0.1,
'space-color': 'rgb(156, 240, 255)'
});
}
});
2024-04-05 17:53:42 +02:00
});
</script>
<div {...$$restProps}>
2024-04-16 22:57:28 +02:00
<div id="map" class="h-full"></div>
2024-04-05 17:53:42 +02:00
</div>
2024-04-10 14:54:35 +02:00
<style lang="postcss">
div :global(.mapboxgl-ctrl) {
@apply shadow-md;
}
div :global(.mapboxgl-ctrl-geocoder) {
@apply flex;
@apply flex-row;
@apply w-fit;
@apply min-w-fit;
@apply items-center;
@apply shadow-md;
}
div :global(.suggestions) {
@apply shadow-md;
}
div :global(.mapboxgl-ctrl-geocoder--icon-search) {
@apply fill-inherit;
@apply relative;
@apply top-0;
@apply left-0;
@apply my-2;
@apply w-[29px];
}
div :global(.mapboxgl-ctrl-geocoder--input) {
@apply relative;
2024-04-12 18:05:26 +02:00
@apply w-64;
2024-04-10 14:54:35 +02:00
@apply py-0;
@apply pl-2;
@apply focus:outline-none;
2024-04-12 18:05:26 +02:00
@apply transition-[width];
@apply duration-200;
2024-04-10 14:54:35 +02:00
}
div :global(.mapboxgl-ctrl-geocoder--collapsed .mapboxgl-ctrl-geocoder--input) {
2024-04-12 18:05:26 +02:00
@apply w-0;
@apply p-0;
2024-04-10 14:54:35 +02:00
}
2024-04-12 15:51:57 +02:00
div :global(.mapboxgl-ctrl-top-right) {
@apply z-10;
2024-04-15 11:25:31 +02:00
@apply flex;
@apply flex-col;
@apply items-end;
@apply h-full;
@apply overflow-hidden;
2024-04-12 15:51:57 +02:00
}
2024-04-19 16:13:08 +02:00
div :global(.mapboxgl-ctrl-bottom-left) {
@apply bottom-10;
}
div :global(.mapboxgl-ctrl-bottom-right) {
@apply bottom-10;
}
2024-04-10 14:54:35 +02:00
</style>