aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/LocationsMap.svelte
blob: 7876659a50a5277212b1467f2961ece11c74484b (plain) (blame)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<script>
	import { onMount, onDestroy } from 'svelte';
	import { browser } from '$app/environment';
	import html2canvas from 'html2canvas';

	import 'maplibre-gl/dist/maplibre-gl.css';

	export let locations;
	let map;
	let mapContainer;
	let markers = [];

	let checkAddress = (location) => {
		if (location['addr:housenumber'] && location['addr:street'] && location['addr:city']) {
			return `${
				location['addr:housenumber'] + ' ' + location['addr:street'] + ', ' + location['addr:city']
			}`;
		} else if (location['addr:street'] && location['addr:city']) {
			return `${location['addr:street'] + ', ' + location['addr:city']}`;
		} else if (location['addr:city']) {
			return `${location['addr:city']}`;
		} else {
			return '';
		}
	};

	function updateLabelVisibility() {
		if (!map) return;
		let zoom = map.getZoom();
		let bounds = map.getBounds();

		for (let marker of markers) {
			let isInView = bounds.contains(marker.getLngLat());
			let popup = marker.getPopup();
			let { tags } = marker;

			if (zoom > 13 && isInView) {
				popup.setHTML(`<span class='block font-bold'>${tags.name}</span>`).addTo(map);
			} else {
				popup.remove();
				popup.setHTML(html(tags));
			}
		}
	}

	let html = (tags) => `${
		tags && tags.name ? `<span class='block font-bold'>${tags.name}</span>` : ''
	}

  <span class='block'>${tags && checkAddress(tags)}</span>

  ${tags && tags.phone ? `<a href='tel:${tags.phone}' class='block'>${tags.phone}</a>` : ''}

  ${
		tags && tags.website
			? `<a href=${tags.website} target="_blank" rel="noreferrer" class='block text-blue-400'>${tags.website}</a>`
			: ''
	}`;

	onMount(() => {
		if (browser) {
			import('maplibre-gl').then(({ default: maplibre }) => {
				map = new maplibre.Map({
					container: mapContainer,
					style: 'https://tiles.stadiamaps.com/styles/stamen_toner.json',
					center: [-123.05, 49.26],
					zoom: 10.5
				});

				map.on('load', () => {
					locations.forEach((location) => {
						if (location['deleted_at']) return;

						let { lat, lon, tags } = location['osm_json'];
						if (!(lat && lon)) return;

						let element = document.createElement('div');
						element.className = 'marker';

						let marker = new maplibre.Marker({ color: '#F7931A' })
							.setLngLat([lon, lat])
							.setPopup(new maplibre.Popup().setHTML(html(tags)))
							.addTo(map);

						marker.getElement().addEventListener('click', () => {
							let h = html(marker.tags);
							let popup = marker.getPopup();
							popup.setHTML(h);
						});

						marker.tags = tags;

						markers.push(marker);
					});

					// updateLabelVisibility();
					// map.on('moveend', updateLabelVisibility);
					// map.on('zoomend', updateLabelVisibility);
				});
			});
		}
	});

	onDestroy(() => {
		if (map) {
			// map.off('moveend', updateLabelVisibility);
			// map.off('zoomend', updateLabelVisibility);
			map.remove();
		}
	});
</script>

<div class="container mx-auto max-w-4xl">
	<div class="flex w-full">
		<div class="mx-auto h-[550px] w-full z-0" bind:this={mapContainer} />
	</div>
</div>

<style>
	:global(.maplibregl-popup-close-button) {
		font-size: 18px;
		padding: 8px;
	}

	:global(.maplibregl-popup :focus) {
		outline: none;
	}

	:global(.maplibregl-ctrl-attrib) {
		display: none;
	}

	:global(.maplibregl-popup-content) {
		padding: 0.5em 1em;
		border-radius: 4px;
		font-size: 14px;
		color: black;
		background-color: white;
		overflow: hidden;
	}
</style>