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;
20 let map = new L.map(container, {
21 attributionControl: (attribution && attribution.length),
22 scrollWheelZoom: true, // !L.Browser.touch,
25 Nominatim_Config.Map_Default_Lat,
26 Nominatim_Config.Map_Default_Lon
28 zoom: Nominatim_Config.Map_Default_Zoom
31 L.tileLayer(Nominatim_Config.Map_Tile_URL, {
32 attribution: attribution
35 if (display_minimap) {
36 let osm2 = new L.TileLayer(Nominatim_Config.Map_Tile_URL, {
39 attribution: attribution
41 new L.Control.MiniMap(osm2, { toggleDisplay: true }).addTo(map);
44 const MapPositionControl = L.Control.extend({
45 options: { position: 'topright' },
46 onAdd: () => { return document.getElementById('show-map-position'); }
48 map.addControl(new MapPositionControl());
53 function mapAction(container) {
54 let map = createMap(container);
56 setMapData(current_result);
66 function parse_and_normalize_geojson_string(part) {
67 // normalize places the geometry into a featurecollection, similar to
68 // https://github.com/mapbox/geojson-normalize
69 var parsed_geojson = {
70 type: 'FeatureCollection',
79 return parsed_geojson;
82 function resetMapData() {
83 let map = get(map_store);
86 dataLayers.forEach(function (layer) {
87 map.removeLayer(layer);
91 function setMapData(aFeature) {
92 let map = get(map_store);
97 if (position_marker) {
98 // We don't need a marker, but L.circle would change radius when you zoom in/out
99 let cm = L.circleMarker(
104 fillColor: '#ff7800',
111 cm.bindTooltip(`Search (${position_marker[0]},${position_marker[1]})`).openTooltip();
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 if (position_marker) { // reverse result
141 circle.bindTooltip('Result').openTooltip();
143 map.addLayer(circle);
144 dataLayers.push(circle);
149 var geojson_layer = L.geoJson(
150 // https://leafletjs.com/reference-1.7.1.html#path-option
151 parse_and_normalize_geojson_string(geojson),
154 return { interactive: false, color: 'blue' };
158 map.addLayer(geojson_layer);
159 dataLayers.push(geojson_layer);
160 map.fitBounds(geojson_layer.getBounds());
161 } else if (lat && lon && position_marker) {
162 map.fitBounds([[lat, lon], position_marker], { padding: [50, 50] });
163 } else if (lat && lon) {
164 map.setView([lat, lon], 10);
168 $: setMapData(current_result);
170 function show_map_position_click(e) {
171 e.target.style.display = 'none';
172 document.getElementById('map-position').style.display = 'block';
177 <div id="map" use:mapAction />
178 <div id="show-map-position" class="leaflet-bar btn btn-sm btn-outline-secondary"
179 on:click|stopPropagation={show_map_position_click}
180 >show map bounds</div>
188 .btn-outline-secondary {
189 background-color: white;
192 .btn-outline-secondary:hover {
196 @media (max-width: 768px) {