]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/components/SearchSectionReverse.svelte
Rebundle latest version
[nominatim-ui.git] / src / components / SearchSectionReverse.svelte
1 <script>
2   import UrlSubmitForm from '../components/UrlSubmitForm.svelte';
3
4   import { zoomLevels } from '../lib/helpers.js';
5   import { map_store, refresh_page } from '../lib/stores.js';
6
7   export let lat = '';
8   export let lon = '';
9   export let zoom = '';
10
11   function gotoCoordinates(newlat, newlon, newzoom) {
12     let params = new URLSearchParams();
13     params.set('lat', newlat);
14     params.set('lon', newlon);
15     params.set('zoom', newzoom || zoom);
16     refresh_page('reverse', params);
17   }
18
19   map_store.subscribe(map => {
20     if (map) {
21       map.on('click', (e) => {
22         let coords = e.latlng.wrap();
23         gotoCoordinates(coords.lat.toFixed(5), coords.lng.toFixed(5));
24       });
25     }
26   });
27
28   // common mistake is to copy&paste latitude and longitude into the 'lat' search box
29   function maybeSplitLatitude(e) {
30     var coords_split = e.target.value.split(',');
31     if (coords_split.length === 2) {
32       document.querySelector('input[name=lat]').value = L.Util.trim(coords_split[0]);
33       document.querySelector('input[name=lon]').value = L.Util.trim(coords_split[1]);
34     }
35   }
36
37 </script>
38
39 <UrlSubmitForm page="reverse">
40   <div class="form-group">
41     <label for="reverse-lat">lat</label>
42     <input id="reverse-lat"
43            name="lat"
44            type="text"
45            class="form-control form-control-sm"
46            placeholder="latitude"
47            pattern="^-?\d+(\.\d+)?$"
48            bind:value={lat}
49            on:change={maybeSplitLatitude} />
50   </div>
51   <div class="form-group">
52     <a id="switch-coords"
53        on:click|preventDefault|stopPropagation={() => gotoCoordinates(lon, lat)}
54        class="btn btn-outline-secondary btn-sm"
55        title="switch lat and lon">&lt;&gt;</a>
56   </div>
57   <div class="form-group">
58     <label for="reverse-lon">lon</label>
59     <input id="reverse-lon"
60            name="lon"
61            type="text"
62            class="form-control form-control-sm"
63            placeholder="longitude"
64            pattern="^-?\d+(\.\d+)?$"
65            bind:value={lon} />
66   </div>
67   <div class="form-group">
68     <label for="reverse-zoom">max zoom</label>
69     <select id="reverse-zoom" name="zoom" class="form-control form-control-sm" bind:value={zoom}>
70       <option value="">---</option>
71       {#each zoomLevels() as zoomTitle, i}
72         <option value="{i}">{i} - {zoomTitle}</option>
73       {/each}
74     </select>
75   </div>
76   <div class="form-group">
77     <button type="submit" class="btn btn-primary btn-sm mx-1">Search</button>
78   </div>
79 </UrlSubmitForm>
80
81 <style>
82   label {
83     font-weight: normal;
84     margin-left: 0.4rem;
85     margin-right: 0.4rem;
86   }
87
88   #switch-coords {
89     font-size: 0.6rem;
90     font-weight: bold;
91     cursor: pointer;
92     padding: 2px;
93     margin: 5px;
94   }
95
96   @media (max-width: 850px) {
97     #reverse-lon, #reverse-lat, #reverse-zoom {
98       width: 8em;
99     }
100   }
101 </style>