feat: Dynamically load leaflet to improve first-screen loading speed
This commit is contained in:
parent
980019bb50
commit
382299d549
|
@ -1,6 +1,4 @@
|
||||||
<script>
|
<script>
|
||||||
import L from 'leaflet';
|
|
||||||
import 'leaflet/dist/leaflet.css';
|
|
||||||
import { onMount, onDestroy } from 'svelte';
|
import { onMount, onDestroy } from 'svelte';
|
||||||
|
|
||||||
let map;
|
let map;
|
||||||
|
@ -13,30 +11,12 @@
|
||||||
|
|
||||||
let markerGroupLayer = null;
|
let markerGroupLayer = null;
|
||||||
|
|
||||||
const setMarkers = (points) => {
|
|
||||||
if (map) {
|
|
||||||
if (markerGroupLayer) {
|
|
||||||
map.removeLayer(markerGroupLayer);
|
|
||||||
}
|
|
||||||
|
|
||||||
let markers = [];
|
|
||||||
for (let point of points) {
|
|
||||||
const marker = L.marker(point.coords).bindPopup(point.content);
|
|
||||||
|
|
||||||
markers.push(marker);
|
|
||||||
}
|
|
||||||
|
|
||||||
markerGroupLayer = L.featureGroup(markers).addTo(map);
|
|
||||||
|
|
||||||
try {
|
|
||||||
map.fitBounds(markerGroupLayer.getBounds(), {
|
|
||||||
maxZoom: Math.max(map.getZoom(), 13)
|
|
||||||
});
|
|
||||||
} catch (error) {}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
|
const [{ default: L }] = await Promise.all([
|
||||||
|
import('leaflet'),
|
||||||
|
import('leaflet/dist/leaflet.css')
|
||||||
|
]);
|
||||||
|
|
||||||
map = L.map(mapElement).setView(setViewLocation ? setViewLocation : [51.505, -0.09], 10);
|
map = L.map(mapElement).setView(setViewLocation ? setViewLocation : [51.505, -0.09], 10);
|
||||||
|
|
||||||
if (setViewLocation) {
|
if (setViewLocation) {
|
||||||
|
@ -53,6 +33,29 @@
|
||||||
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
|
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
|
||||||
}).addTo(map);
|
}).addTo(map);
|
||||||
|
|
||||||
|
const setMarkers = (points) => {
|
||||||
|
if (map) {
|
||||||
|
if (markerGroupLayer) {
|
||||||
|
map.removeLayer(markerGroupLayer);
|
||||||
|
}
|
||||||
|
|
||||||
|
let markers = [];
|
||||||
|
for (let point of points) {
|
||||||
|
const marker = L.marker(point.coords).bindPopup(point.content);
|
||||||
|
|
||||||
|
markers.push(marker);
|
||||||
|
}
|
||||||
|
|
||||||
|
markerGroupLayer = L.featureGroup(markers).addTo(map);
|
||||||
|
|
||||||
|
try {
|
||||||
|
map.fitBounds(markerGroupLayer.getBounds(), {
|
||||||
|
maxZoom: Math.max(map.getZoom(), 13)
|
||||||
|
});
|
||||||
|
} catch (error) {}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
setMarkers(points);
|
setMarkers(points);
|
||||||
|
|
||||||
map.on('click', (e) => {
|
map.on('click', (e) => {
|
||||||
|
|
Loading…
Reference in New Issue