]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/assets/js/searchpage.js
On reverse when red and blue circle is on map, make sure red is on top
[nominatim-ui.git] / src / assets / js / searchpage.js
1
2 // *********************************************************
3 // FORWARD/REVERSE SEARCH PAGE
4 // *********************************************************
5
6
7 function display_map_position(mouse_lat_lng) {
8   //
9   if (mouse_lat_lng) {
10     mouse_lat_lng = map.wrapLatLng(mouse_lat_lng);
11   }
12
13   html_mouse = 'mouse position ' + (mouse_lat_lng ? [mouse_lat_lng.lat.toFixed(5), mouse_lat_lng.lng.toFixed(5)].join(',') : '-');
14   html_click = 'last click: ' + (last_click_latlng ? [last_click_latlng.lat.toFixed(5), last_click_latlng.lng.toFixed(5)].join(',') : '-');
15
16   html_center = 'map center: '
17     + map.getCenter().lat.toFixed(5) + ',' + map.getCenter().lng.toFixed(5)
18     + ' <a target="_blank" href="' + map_link_to_osm() + '">view on osm.org</a>';
19
20   html_zoom = 'map zoom: ' + map.getZoom();
21
22   html_viewbox = 'viewbox: ' + map_viewbox_as_string();
23
24   $('#map-position-inner').html([html_center, html_zoom, html_viewbox, html_click, html_mouse].join('<br/>'));
25
26   var center_lat_lng = map.wrapLatLng(map.getCenter());
27   var reverse_params = {
28     lat: center_lat_lng.lat.toFixed(5),
29     lon: center_lat_lng.lng.toFixed(5)
30     // zoom: 2,
31     // format: 'html'
32   };
33   $('#switch-to-reverse').attr('href', 'reverse.html?' + $.param(reverse_params));
34
35   $('input#use_viewbox').trigger('change');
36 }
37
38 function init_map_on_search_page(is_reverse_search, nominatim_results, request_lat, request_lon, init_zoom) {
39   //
40   map = new L.map('map', {
41     // center: [nominatim_map_init.lat, nominatim_map_init.lon],
42     // zoom:   nominatim_map_init.zoom,
43     attributionControl: (get_config_value('Map_Tile_Attribution') && get_config_value('Map_Tile_Attribution').length),
44     scrollWheelZoom: true, // !L.Browser.touch,
45     touchZoom: false,
46   });
47
48
49   L.tileLayer(get_config_value('Map_Tile_URL'), {
50     // moved to footer
51     attribution: (get_config_value('Map_Tile_Attribution') || null ) // '&copy; <a href="https://osm.org/copyright">OpenStreetMap</a> contributors'
52   }).addTo(map);
53
54   // console.log(Nominatim_Config);
55
56   map.setView([request_lat, request_lon], init_zoom);
57
58   var osm2 = new L.TileLayer(get_config_value('Map_Tile_URL'), { minZoom: 0, maxZoom: 13, attribution: (get_config_value('Map_Tile_Attribution') || null ) });
59   new L.Control.MiniMap(osm2, { toggleDisplay: true }).addTo(map);
60
61   if (is_reverse_search) {
62     // We don't need a marker, but an L.circle instance changes radius once you zoom in/out
63     var cm = L.circleMarker(
64       [request_lat, request_lon],
65       {
66         radius: 5,
67         weight: 2,
68         fillColor: '#ff7800',
69         color: 'red',
70         opacity: 0.75,
71         zIndexOffset: 100,
72         clickable: false
73       }
74     );
75     cm.addTo(map);
76   }
77
78   var MapPositionControl = L.Control.extend({
79     options: {
80       position: 'topright'
81     },
82     onAdd: function (/* map */) {
83       var container = L.DomUtil.create('div', 'my-custom-control');
84
85       $(container).text('show map bounds').addClass('leaflet-bar btn btn-sm btn-default').on('click', function (e) {
86         e.preventDefault();
87         e.stopPropagation();
88         $('#map-position').show();
89         $(container).hide();
90       });
91       $('#map-position-close a').on('click', function (e) {
92         e.preventDefault();
93         e.stopPropagation();
94         $('#map-position').hide();
95         $(container).show();
96       });
97
98       return container;
99     }
100   });
101
102   map.addControl(new MapPositionControl());
103
104
105
106
107
108   function update_viewbox_field() {
109     // hidden HTML field
110     $('input[name=viewbox]').val($('input#use_viewbox').prop('checked') ? map_viewbox_as_string() : '');
111   }
112
113   map.on('move', function () {
114     display_map_position();
115     update_viewbox_field();
116   });
117
118   map.on('mousemove', function (e) {
119     display_map_position(e.latlng);
120   });
121
122   map.on('click', function (e) {
123     last_click_latlng = e.latlng;
124     display_map_position();
125   });
126
127   map.on('load', function () {
128     display_map_position();
129   });
130
131   $('input#use_viewbox').on('change', function () {
132     update_viewbox_field();
133   });
134
135
136
137
138   function get_result_element(position) {
139     return $('.result').eq(position);
140   }
141   function marker_for_result(result) {
142     return L.marker([result.lat, result.lon], { riseOnHover: true, title: result.name });
143   }
144   function circle_for_result(result) {
145     var cm_style = {
146       radius: 10,
147       weight: 2,
148       fillColor: '#ff7800',
149       color: 'blue',
150       opacity: 0.75,
151       clickable: !is_reverse_search
152     };
153     return L.circleMarker([result.lat, result.lon], cm_style);
154   }
155
156   var layerGroup = new L.layerGroup().addTo(map);
157
158   function highlight_result(position, bool_focus) {
159     var result = nominatim_results[position];
160     if (!result) { return; }
161     var result_el = get_result_element(position);
162
163     $('.result').removeClass('highlight');
164     result_el.addClass('highlight');
165
166     layerGroup.clearLayers();
167
168     if (result.lat) {
169       var circle = circle_for_result(result);
170       circle.on('click', function () {
171         highlight_result(position);
172       });
173       layerGroup.addLayer(circle);
174     }
175
176     if (result.boundingbox) {
177       var bounds = [
178         [result.boundingbox[0] * 1, result.boundingbox[2] * 1],
179         [result.boundingbox[1] * 1, result.boundingbox[3] * 1]
180       ];
181       map.fitBounds(bounds);
182
183       if (result.geojson && result.geojson.type.match(/(Polygon)|(Line)/)) {
184         //
185         var geojson_layer = L.geoJson(
186           parse_and_normalize_geojson_string(result.geojson),
187           {
188             // https://leafletjs.com/reference-1.0.3.html#path-option
189             style: function (feature) {
190               return { interactive: false, color: 'blue' };
191             }
192           }
193         );
194         layerGroup.addLayer(geojson_layer);
195       }
196       // else {
197       //     var layer = L.rectangle(bounds, {color: "#ff7800", weight: 1} );
198       //     layerGroup.addLayer(layer);
199       // }
200     } else {
201       var result_coord = L.latLng(result.lat, result.lon);
202       if (result_coord) {
203         if (is_reverse_search) {
204           // console.dir([result_coord, [request_lat, request_lon]]);
205           // make sure the search coordinates are in the map view as well
206           map.fitBounds(
207             [result_coord, [request_lat, request_lon]],
208             {
209               padding: [50, 50],
210               maxZoom: map.getZoom()
211             }
212           );
213         } else {
214           map.panTo(result_coord, result.zoom || get_config_value('Map_Default_Zoom'));
215         }
216       }
217     }
218     if (bool_focus) {
219       $('#map').focus();
220     }
221   }
222
223
224   $('.result').on('click', function () {
225     highlight_result($(this).data('position'), true);
226   });
227
228   if (is_reverse_search) {
229     map.on('click', function (e) {
230       $('form input[name=lat]').val(e.latlng.lat);
231       $('form input[name=lon]').val(e.latlng.wrap().lng);
232       $('form').submit();
233     });
234
235     $('#switch-coords').on('click', function (e) {
236       e.preventDefault();
237       e.stopPropagation();
238       var lat = $('form input[name=lat]').val();
239       var lon = $('form input[name=lon]').val();
240       $('form input[name=lat]').val(lon);
241       $('form input[name=lon]').val(lat);
242       $('form').submit();
243     });
244   }
245
246   highlight_result(0, false);
247
248   // common mistake is to copy&paste latitude and longitude into the 'lat' search box
249   $('form input[name=lat]').on('change', function () {
250     var coords = $(this).val().split(',');
251     if (coords.length === 2) {
252       $(this).val(L.Util.trim(coords[0]));
253       $(this).siblings('input[name=lon]').val(L.Util.trim(coords[1]));
254     }
255   });
256 }
257
258
259
260
261
262
263
264 jQuery(document).ready(function () {
265   //
266   if (!$('#search-page,#reverse-page').length) { return; }
267
268   var is_reverse_search = !!($('#reverse-page').length);
269   var endpoint = is_reverse_search ? 'reverse' : 'search';
270
271
272   var search_params = new URLSearchParams(location.search);
273
274   // return view('search', [
275   //     'sQuery' => $sQuery,
276   //     'bAsText' => '',
277   //     'sViewBox' => '',
278   //     'aSearchResults' => $aSearchResults,
279   //     'sMoreURL' => 'example.com',
280   //     'sDataDate' => $this->fetch_status_date(),
281   //     'sApiURL' => $url
282   // ]);
283
284
285   console.log(search_params);
286
287   if (is_reverse_search) {
288     var api_request_params = {
289       lat: search_params.get('lat'),
290       lon: search_params.get('lon'),
291       zoom: (search_params.get('zoom') > 1 ? search_params.get('zoom') : get_config_value('Reverse_Default_Search_Zoom')),
292       format: 'jsonv2'
293     };
294
295     var context = {
296       // aPlace: aPlace,
297       fLat: api_request_params.lat,
298       fLon: api_request_params.lon,
299       iZoom: (search_params.get('zoom') > 1 ? api_request_params.zoom : get_config_value('Reverse_Default_Search_Zoom'))
300     };
301
302
303     if (api_request_params.lat && api_request_params.lon) {
304
305       fetch_from_api('reverse', api_request_params, function (aPlace) {
306
307         if (aPlace.error) {
308           aPlace = null;
309         }
310
311         context.aPlace = aPlace;
312
313         render_template($('main'), 'reversepage-template', context);
314
315         init_map_on_search_page(
316           is_reverse_search,
317           [aPlace],
318           api_request_params.lat,
319           api_request_params.lon,
320           api_request_params.zoom
321         );
322
323         update_data_date();
324       });
325     } else {
326       render_template($('main'), 'reversepage-template', context);
327
328       init_map_on_search_page(
329         is_reverse_search,
330         [],
331         get_config_value('Map_Default_Lat'),
332         get_config_value('Map_Default_Lon'),
333         get_config_value('Map_Default_Zoom')
334       );
335     }
336
337   } else {
338     var api_request_params = {
339       q: search_params.get('q'),
340       polygon_geojson: search_params.get('polygon_geojson') ? 1 : 0,
341       viewbox: search_params.get('viewbox'),
342       format: 'jsonv2'
343     };
344
345     var context = {
346       // aSearchResults: aResults,
347       sQuery: api_request_params.q,
348       sViewBox: '',
349       env: Nominatim_Config,
350       sMoreURL: ''
351     };
352
353     if (api_request_params.q) {
354
355       fetch_from_api('search', api_request_params, function (aResults) {
356
357         context.aSearchResults = aResults;
358
359         render_template($('main'), 'searchpage-template', context);
360
361         init_map_on_search_page(is_reverse_search, aResults, get_config_value('Map_Default_Lat'), get_config_value('Map_Default_Lon'), get_config_value('Map_Default_Zoom'));
362
363         $('#q').focus();
364
365         update_data_date();
366       });
367     } else {
368       render_template($('main'), 'searchpage-template', context);
369
370       init_map_on_search_page(is_reverse_search, [], get_config_value('Map_Default_Lat'), get_config_value('Map_Default_Lon'), get_config_value('Map_Default_Zoom'));
371     }
372   }
373 });