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