-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move map into components and make polylines dynamic
- Loading branch information
1 parent
72e35bb
commit f785c5f
Showing
3 changed files
with
139 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<script> | ||
import { createEventDispatcher, setContext } from 'svelte'; | ||
import L from 'leaflet?client'; | ||
import 'leaflet/dist/leaflet.css'; | ||
export let height = '100%'; | ||
export let width = '100%'; | ||
// Must set either bounds, or view and zoom. | ||
export let bounds = undefined; | ||
export let view = undefined; | ||
export let zoom = undefined; | ||
let mapProp = undefined; | ||
export { mapProp as map }; | ||
export const invalidateSize = () => map?.invalidateSize(); | ||
const dispatch = createEventDispatcher(); | ||
let map; | ||
$: mapProp = map; | ||
export const getMap = () => map; | ||
setContext('layerGroup', getMap); | ||
setContext('layer', getMap); | ||
setContext('map', getMap); | ||
function createLeaflet(node) { | ||
map = L.map(node).on('zoom', (e) => dispatch('zoom', e)); | ||
if(bounds) { | ||
map.fitBounds(bounds) | ||
} else { | ||
map.setView(view, zoom); | ||
} | ||
L.tileLayer('https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}' + (L.Browser.retina ? '@2x.png' : '.png'), { | ||
attribution:'© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, © <a href="https://carto.com/attributions">CARTO</a>', | ||
subdomains: 'abcd', | ||
maxZoom: 20, | ||
minZoom: 0 | ||
}).addTo(map); | ||
return { | ||
destroy() { | ||
map.remove(); | ||
map = undefined; | ||
}, | ||
}; | ||
} | ||
$: if(map) { | ||
if(bounds) { | ||
map.fitBounds(bounds) | ||
} else { | ||
map.setView(view, zoom); | ||
} | ||
} | ||
</script> | ||
|
||
<style> | ||
:global(.leaflet-control-container) { | ||
position: static; | ||
} | ||
</style> | ||
|
||
<div style="height:{height};width:{width}" use:createLeaflet> | ||
{#if map} | ||
<slot {map} /> | ||
{/if} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<script lang="ts"> | ||
import L from 'leaflet?client'; | ||
import {createEventDispatcher, getContext, setContext, onDestroy} from 'svelte'; | ||
export let latLngs; | ||
export let options; | ||
const dispatch = createEventDispatcher(); | ||
// let layerPane = pane || getContext('pane'); | ||
// @ts-ignore | ||
let layerGroup: L.LayerGroup = getContext('layerGroup')(); | ||
export let line: L.Polyline = new L.Polyline(latLngs, options) | ||
.on('click', (e) => dispatch('click', e)) | ||
.on('mouseover', (e) => dispatch('mouseover', e)) | ||
.on('mouseout', (e) => dispatch('mouseout', e)) | ||
.addTo(layerGroup); | ||
setContext('layer', () => line); | ||
onDestroy(() => { | ||
line.remove(); | ||
line = undefined; | ||
}); | ||
$: line.setStyle(options); | ||
$: { | ||
line.setLatLngs(latLngs); | ||
line.redraw(); | ||
} | ||
</script> | ||
|
||
<slot /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters