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' && Nominatim_Config.Map_Default_Bounds) {
32 map.fitBounds(Nominatim_Config.Map_Default_Bounds);
35 if (attribution && attribution.length) {
36 L.control.attribution({ prefix: '<a href="https://leafletjs.com/">Leaflet</a>' }).addTo(map);
39 L.tileLayer(Nominatim_Config.Map_Tile_URL, {
40 attribution: attribution
43 if (display_minimap) {
44 let osm2 = new L.TileLayer(Nominatim_Config.Map_Tile_URL, {
47 attribution: attribution
49 new L.Control.MiniMap(osm2, { toggleDisplay: true }).addTo(map);
52 const MapPositionControl = L.Control.extend({
53 options: { position: 'topright' },
54 onAdd: () => { return document.getElementById('show-map-position'); }
56 map.addControl(new MapPositionControl());
61 function mapAction(container) {
62 let map = createMap(container);
64 setMapData(current_result);
74 function parse_and_normalize_geojson_string(part) {
75 // normalize places the geometry into a featurecollection, similar to
76 // https://github.com/mapbox/geojson-normalize
77 var parsed_geojson = {
78 type: 'FeatureCollection',
87 return parsed_geojson;
90 function resetMapData() {
91 let map = get(map_store);
94 dataLayers.forEach(function (layer) {
95 map.removeLayer(layer);
99 function setMapData(aFeature) {
100 let map = get(map_store);
101 if (!map) { return; }
105 if (position_marker) {
106 // We don't need a marker, but L.circle would change radius when you zoom in/out
107 let cm = L.circleMarker(
112 fillColor: '#ff7800',
119 cm.bindTooltip(`Search (${position_marker[0]},${position_marker[1]})`).openTooltip();
124 var search_params = new URLSearchParams(window.location.search);
125 var viewbox = search_params.get('viewbox');
127 let coords = viewbox.split(','); // <x1>,<y1>,<x2>,<y2>
128 let bounds = L.latLngBounds([coords[1], coords[0]], [coords[3], coords[2]]);
129 L.rectangle(bounds, {
138 if (!aFeature) return;
140 let lat = aFeature.centroid ? aFeature.centroid.coordinates[1] : aFeature.lat;
141 let lon = aFeature.centroid ? aFeature.centroid.coordinates[0] : aFeature.lon;
142 let geojson = aFeature.geometry || aFeature.geojson;
145 let circle = L.circleMarker([lat, lon], {
146 radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75
148 if (position_marker) { // reverse result
149 circle.bindTooltip('Result').openTooltip();
151 map.addLayer(circle);
152 dataLayers.push(circle);
157 var geojson_layer = L.geoJson(
158 // https://leafletjs.com/reference-1.7.1.html#path-option
159 parse_and_normalize_geojson_string(geojson),
162 return { interactive: false, color: 'blue' };
166 map.addLayer(geojson_layer);
167 dataLayers.push(geojson_layer);
168 map.fitBounds(geojson_layer.getBounds());
169 } else if (lat && lon && position_marker) {
170 map.fitBounds([[lat, lon], position_marker], { padding: [50, 50] });
171 } else if (lat && lon) {
172 map.setView([lat, lon], 10);
176 $: setMapData(current_result);
178 function show_map_position_click(e) {
179 e.target.style.display = 'none';
180 document.getElementById('map-position').style.display = 'block';
185 <div id="map" use:mapAction />
186 <div id="show-map-position" class="leaflet-bar btn btn-sm btn-outline-secondary"
187 on:click|stopPropagation={show_map_position_click}
188 >show map bounds</div>
196 .btn-outline-secondary {
197 background-color: white;
200 .btn-outline-secondary:hover {
204 @media (max-width: 768px) {