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 L.circle would change radius when 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, {
130 if (!aFeature) return;
132 let lat = aFeature.centroid ? aFeature.centroid.coordinates[1] : aFeature.lat;
133 let lon = aFeature.centroid ? aFeature.centroid.coordinates[0] : aFeature.lon;
134 let geojson = aFeature.geometry || aFeature.geojson;
137 let circle = L.circleMarker([lat, lon], {
138 radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75
140 map.addLayer(circle);
141 dataLayers.push(circle);
146 var geojson_layer = L.geoJson(
147 // https://leafletjs.com/reference-1.7.1.html#path-option
148 parse_and_normalize_geojson_string(geojson),
151 return { interactive: false, color: 'blue' };
155 map.addLayer(geojson_layer);
156 dataLayers.push(geojson_layer);
157 map.fitBounds(geojson_layer.getBounds());
158 } else if (lat && lon && position_marker) {
159 map.fitBounds([[lat, lon], position_marker], { padding: [50, 50] });
160 } else if (lat && lon) {
161 map.setView([lat, lon], 10);
165 $: setMapData(current_result);
167 function show_map_position_click(e) {
168 e.target.style.display = 'none';
169 document.getElementById('map-position').style.display = 'block';
174 <div id="map" use:mapAction />
175 <div id="show-map-position" class="leaflet-bar btn btn-sm btn-outline-secondary"
176 on:click|stopPropagation={show_map_position_click}
177 >show map bounds</div>
185 .btn-outline-secondary {
186 background-color: white;
189 .btn-outline-secondary:hover {
193 @media (max-width: 768px) {