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 { map_store } from '../lib/stores.js';
10 import MapPosition from '../components/MapPosition.svelte';
12 export let display_minimap = false;
13 export let current_result = null;
14 export let position_marker = null;
18 function createMap(container) {
19 const attribution = Nominatim_Config.Map_Tile_Attribution;
21 let map = new L.map(container, {
22 attributionControl: false,
23 scrollWheelZoom: true, // !L.Browser.touch,
26 Nominatim_Config.Map_Default_Lat,
27 Nominatim_Config.Map_Default_Lon
29 zoom: Nominatim_Config.Map_Default_Zoom
31 if (typeof Nominatim_Config.Map_Default_Bounds !== 'undefined'
32 && Nominatim_Config.Map_Default_Bounds) {
33 map.fitBounds(Nominatim_Config.Map_Default_Bounds);
36 if (attribution && attribution.length) {
37 L.control.attribution({ prefix: '<a href="https://leafletjs.com/">Leaflet</a>' }).addTo(map);
40 L.tileLayer(Nominatim_Config.Map_Tile_URL, {
41 attribution: attribution
44 if (display_minimap) {
45 let osm2 = new L.TileLayer(Nominatim_Config.Map_Tile_URL, {
48 attribution: attribution
50 new L.Control.MiniMap(osm2, { toggleDisplay: true }).addTo(map);
53 const MapPositionControl = L.Control.extend({
54 options: { position: 'topright' },
55 onAdd: () => { return document.getElementById('show-map-position'); }
57 map.addControl(new MapPositionControl());
62 function mapAction(container) {
63 let map = createMap(container);
65 setMapData(current_result);
75 function parse_and_normalize_geojson_string(part) {
76 // normalize places the geometry into a featurecollection, similar to
77 // https://github.com/mapbox/geojson-normalize
78 var parsed_geojson = {
79 type: 'FeatureCollection',
88 return parsed_geojson;
91 function resetMapData() {
92 let map = get(map_store);
95 dataLayers.forEach(function (layer) {
96 map.removeLayer(layer);
100 function setMapData(aFeature) {
101 let map = get(map_store);
102 if (!map) { return; }
106 if (position_marker) {
107 // We don't need a marker, but L.circle would change radius when you zoom in/out
108 let cm = L.circleMarker(
113 fillColor: '#ff7800',
120 cm.bindTooltip(`Search (${position_marker[0]},${position_marker[1]})`).openTooltip();
125 var search_params = new URLSearchParams(window.location.search);
126 var viewbox = search_params.get('viewbox');
128 let coords = viewbox.split(','); // <x1>,<y1>,<x2>,<y2>
129 let bounds = L.latLngBounds([coords[1], coords[0]], [coords[3], coords[2]]);
130 L.rectangle(bounds, {
139 if (!aFeature) return;
141 let lat = aFeature.centroid ? aFeature.centroid.coordinates[1] : aFeature.lat;
142 let lon = aFeature.centroid ? aFeature.centroid.coordinates[0] : aFeature.lon;
143 let geojson = aFeature.geometry || aFeature.geojson;
146 let circle = L.circleMarker([lat, lon], {
147 radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75
149 if (position_marker) { // reverse result
150 circle.bindTooltip('Result').openTooltip();
152 map.addLayer(circle);
153 dataLayers.push(circle);
158 var geojson_layer = L.geoJson(
159 // https://leafletjs.com/reference-1.7.1.html#path-option
160 parse_and_normalize_geojson_string(geojson),
163 return { interactive: false, color: 'blue' };
167 map.addLayer(geojson_layer);
168 dataLayers.push(geojson_layer);
169 map.fitBounds(geojson_layer.getBounds());
170 } else if (lat && lon && position_marker) {
171 map.fitBounds([[lat, lon], position_marker], { padding: [50, 50] });
172 } else if (lat && lon) {
173 map.setView([lat, lon], 10);
177 $: setMapData(current_result);
179 function show_map_position_click(e) {
180 e.target.style.display = 'none';
181 document.getElementById('map-position').style.display = 'block';
186 <div id="map" use:mapAction />
187 <div id="show-map-position" class="leaflet-bar btn btn-sm btn-outline-secondary"
188 on:click|stopPropagation={show_map_position_click}
189 >show map bounds</div>
197 .btn-outline-secondary {
198 background-color: white;
201 .btn-outline-secondary:hover {
205 @media (max-width: 768px) {