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',
115 var search_params = new URLSearchParams(window.location.search);
116 var viewbox = search_params.get('viewbox');
118 let coords = viewbox.split(','); // <x1>,<y1>,<x2>,<y2>
119 let bounds = L.latLngBounds([coords[1], coords[0]], [coords[3], coords[2]]);
120 L.rectangle(bounds, {
129 if (!aFeature) return;
131 let lat = aFeature.centroid ? aFeature.centroid.coordinates[1] : aFeature.lat;
132 let lon = aFeature.centroid ? aFeature.centroid.coordinates[0] : aFeature.lon;
133 let geojson = aFeature.geometry || aFeature.geojson;
136 let circle = L.circleMarker([lat, lon], {
137 radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75
139 map.addLayer(circle);
140 dataLayers.push(circle);
145 var geojson_layer = L.geoJson(
146 // https://leafletjs.com/reference-1.7.1.html#path-option
147 parse_and_normalize_geojson_string(geojson),
150 return { interactive: false, color: 'blue' };
154 map.addLayer(geojson_layer);
155 dataLayers.push(geojson_layer);
156 map.fitBounds(geojson_layer.getBounds());
157 } else if (lat && lon && position_marker) {
158 map.fitBounds([[lat, lon], position_marker], { padding: [50, 50] });
159 } else if (lat && lon) {
160 map.setView([lat, lon], 10);
164 $: setMapData(current_result);
166 function show_map_position_click(e) {
167 e.target.style.display = 'none';
168 document.getElementById('map-position').style.display = 'block';
173 <div id="map" use:mapAction />
174 <div id="show-map-position" class="leaflet-bar btn btn-sm btn-outline-secondary"
175 on:click|stopPropagation={show_map_position_click}
176 >show map bounds</div>
184 .btn-outline-secondary {
185 background-color: white;
188 .btn-outline-secondary:hover {
192 @media (max-width: 768px) {