3 import * as L from 'leaflet';
4 import 'leaflet-minimap';
5 import 'leaflet/dist/leaflet.css';
6 import 'leaflet-minimap/dist/Control.MiniMap.min.css';
8 import { get } from 'svelte/store';
9 import { get_config_value } from '../lib/config_reader.js';
10 import { map_store } from '../lib/stores.js';
11 import MapPosition from '../components/MapPosition.svelte';
13 export let display_minimap = false;
14 export let current_result = null;
15 export let position_marker = null;
19 function createMap(container) {
20 const attribution = get_config_value('Map_Tile_Attribution') || null;
21 let map = new L.map(container, {
22 attributionControl: (attribution && attribution.length),
23 scrollWheelZoom: true, // !L.Browser.touch,
26 get_config_value('Map_Default_Lat'),
27 get_config_value('Map_Default_Lon')
29 zoom: get_config_value('Map_Default_Zoom')
32 L.tileLayer(get_config_value('Map_Tile_URL'), {
33 attribution: attribution
36 if (display_minimap) {
37 let osm2 = new L.TileLayer(get_config_value('Map_Tile_URL'), {
40 attribution: attribution
42 new L.Control.MiniMap(osm2, { toggleDisplay: true }).addTo(map);
45 const MapPositionControl = L.Control.extend({
46 options: { position: 'topright' },
47 onAdd: () => { return document.getElementById('show-map-position'); }
49 map.addControl(new MapPositionControl());
54 function mapAction(container) {
55 let map = createMap(container);
57 setMapData(current_result);
67 function parse_and_normalize_geojson_string(part) {
68 // normalize places the geometry into a featurecollection, similar to
69 // https://github.com/mapbox/geojson-normalize
70 var parsed_geojson = {
71 type: 'FeatureCollection',
80 return parsed_geojson;
83 function resetMapData() {
84 let map = get(map_store);
87 dataLayers.forEach(function (layer) {
88 map.removeLayer(layer);
92 function setMapData(aFeature) {
93 let map = get(map_store);
98 if (position_marker) {
99 // We don't need a marker, but an L.circle instance changes radius once you zoom in/out
100 let cm = L.circleMarker(
105 fillColor: '#ff7800',
116 var search_params = new URLSearchParams(window.location.search);
117 var viewbox = search_params.get('viewbox');
119 let coords = viewbox.split(','); // <x1>,<y1>,<x2>,<y2>
120 let bounds = L.latLngBounds([coords[1], coords[0]], [coords[3], coords[2]]);
121 L.rectangle(bounds, {
131 if (!aFeature) { return; }
133 let lat = aFeature.centroid ? aFeature.centroid.coordinates[1] : aFeature.lat;
134 let lon = aFeature.centroid ? aFeature.centroid.coordinates[0] : aFeature.lon;
135 let geojson = aFeature.geometry || aFeature.geojson;
138 let circle = L.circleMarker([lat, lon], {
139 radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75
141 map.addLayer(circle);
142 dataLayers.push(circle);
147 var geojson_layer = L.geoJson(
148 // https://leafletjs.com/reference-1.0.3.html#path-option
149 parse_and_normalize_geojson_string(geojson),
152 return { interactive: false, color: 'blue' };
156 map.addLayer(geojson_layer);
157 dataLayers.push(geojson_layer);
158 map.fitBounds(geojson_layer.getBounds());
159 } else if (lat && lon) {
160 map.setView([lat, lon], 10);
166 $: setMapData(current_result);
168 function show_map_position_click(e) {
169 e.target.style.display = 'none';
170 document.getElementById('map-position').style.display = 'block';
175 <div id="map" use:mapAction />
176 <div id="show-map-position" class="leaflet-bar btn btn-sm btn-outline-secondary"
177 on:click|stopPropagation={show_map_position_click}
178 >show map bounds</div>
186 .btn-outline-secondary {
187 background-color: white;
190 .btn-outline-secondary:hover {
194 @media (max-width: 768px) {