|
1 | 1 | <script lang="ts"> |
2 | | - import Geolocation from "ol/Geolocation" |
3 | | - import type { Coordinate } from "ol/coordinate" |
4 | | - import Icon from "ol/style/Icon" |
5 | | - import Style from "ol/style/Style" |
6 | | - import Feature from "../ol/Feature.svelte" |
7 | | - import { getOlContext } from "../ol/Map.svelte" |
8 | | - import VectorLayer from "../ol/VectorLayer.svelte" |
9 | | - import MapControl from "./MapControl.svelte" |
| 2 | + import Geolocation from 'ol/Geolocation' |
| 3 | + import type { Coordinate } from 'ol/coordinate' |
| 4 | + import Icon from 'ol/style/Icon' |
| 5 | + import Style from 'ol/style/Style' |
| 6 | + import Feature from '../ol/Feature.svelte' |
| 7 | + import { getOlContext } from '../ol/Map.svelte' |
| 8 | + import VectorLayer from '../ol/VectorLayer.svelte' |
| 9 | + import MapControl from './MapControl.svelte' |
10 | 10 |
|
11 | | - const { map } = getOlContext(); |
| 11 | + const { map } = getOlContext() |
12 | 12 |
|
13 | | - let tracking = false; |
14 | | - let position: Coordinate; |
| 13 | + let tracking = false |
| 14 | + let position: Coordinate |
15 | 15 |
|
16 | 16 | let geolocation = new Geolocation({ |
17 | 17 | projection: map.getView().getProjection(), |
18 | 18 | trackingOptions: { |
19 | | - enableHighAccuracy: true |
20 | | - } |
21 | | - }); |
22 | | - geolocation.on("change", (e) => { |
23 | | - position = geolocation.getPosition(); |
24 | | - }); |
| 19 | + enableHighAccuracy: true, |
| 20 | + }, |
| 21 | + }) |
| 22 | + geolocation.on('change', (e) => { |
| 23 | + const locatedPos = geolocation.getPosition() |
| 24 | + if (!locatedPos) return |
| 25 | + position = locatedPos |
| 26 | + }) |
25 | 27 |
|
26 | 28 | $: { |
27 | 29 | if (tracking) { |
28 | | - geolocation.once("change", () => |
29 | | - map.getView().setCenter(geolocation.getPosition()) |
30 | | - ); |
| 30 | + geolocation.once('change', () => |
| 31 | + map.getView().setCenter(geolocation.getPosition()), |
| 32 | + ) |
31 | 33 | } |
32 | 34 |
|
33 | | - geolocation.setTracking(tracking); |
| 35 | + geolocation.setTracking(tracking) |
34 | 36 | } |
35 | 37 | </script> |
36 | 38 |
|
37 | 39 | <MapControl> |
38 | 40 | <button |
39 | | - on:click={() => tracking = !tracking} |
40 | | - class={`map-button ${tracking && 'hover:bg-primary-hover hover:text-background'}`}> |
| 41 | + on:click={() => (tracking = !tracking)} |
| 42 | + class={`map-button ${ |
| 43 | + tracking && 'hover:bg-primary-hover hover:text-background' |
| 44 | + }`} |
| 45 | + > |
41 | 46 | ⬊ |
42 | 47 | </button> |
43 | 48 | </MapControl> |
|
50 | 55 | image: new Icon({ |
51 | 56 | anchor: [0.5, 0.5], |
52 | 57 | opacity: 0.9, |
53 | | - imgSize: [600, 600], |
| 58 | + size: [600, 600], |
54 | 59 | scale: 0.08, |
55 | 60 | color: '#578dfF', |
56 | 61 | src: '/icons/location-indicator.svg', |
57 | 62 | }), |
58 | | - })} /> |
| 63 | + })} |
| 64 | + /> |
59 | 65 | {/if} |
60 | 66 | </VectorLayer> |
0 commit comments