]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/assets/js/searchpage.js
Set HTML title for all pages
[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   } else {
77     var search_params = new URLSearchParams(location.search);
78     var viewbox = search_params.get('viewbox');
79     if (viewbox) {
80       var coords = viewbox.split(','); // <x1>,<y1>,<x2>,<y2>
81       var bounds = L.latLngBounds([coords[1], coords[0]], [coords[3], coords[2]]);
82       L.rectangle(bounds, {color: "#69d53e", weight: 3, dashArray: '5 5', opacity: 0.8, fill: false}).addTo(map);
83     }
84   }
85
86   var MapPositionControl = L.Control.extend({
87     options: {
88       position: 'topright'
89     },
90     onAdd: function (/* map */) {
91       var container = L.DomUtil.create('div', 'my-custom-control');
92
93       $(container).text('show map bounds').addClass('leaflet-bar btn btn-sm btn-default').on('click', function (e) {
94         e.preventDefault();
95         e.stopPropagation();
96         $('#map-position').show();
97         $(container).hide();
98       });
99       $('#map-position-close a').on('click', function (e) {
100         e.preventDefault();
101         e.stopPropagation();
102         $('#map-position').hide();
103         $(container).show();
104       });
105
106       return container;
107     }
108   });
109
110   map.addControl(new MapPositionControl());
111
112
113
114
115
116   function update_viewbox_field() {
117     // hidden HTML field
118     $('input[name=viewbox]').val($('input#use_viewbox').prop('checked') ? map_viewbox_as_string() : '');
119   }
120
121   map.on('move', function () {
122     display_map_position();
123     update_viewbox_field();
124   });
125
126   map.on('mousemove', function (e) {
127     display_map_position(e.latlng);
128   });
129
130   map.on('click', function (e) {
131     last_click_latlng = e.latlng;
132     display_map_position();
133   });
134
135   map.on('load', function () {
136     display_map_position();
137   });
138
139   $('input#use_viewbox').on('change', function () {
140     update_viewbox_field();
141   });
142
143
144
145
146   function get_result_element(position) {
147     return $('.result').eq(position);
148   }
149   function marker_for_result(result) {
150     return L.marker([result.lat, result.lon], { riseOnHover: true, title: result.name });
151   }
152   function circle_for_result(result) {
153     var cm_style = {
154       radius: 10,
155       weight: 2,
156       fillColor: '#ff7800',
157       color: 'blue',
158       opacity: 0.75,
159       clickable: !is_reverse_search
160     };
161     return L.circleMarker([result.lat, result.lon], cm_style);
162   }
163
164   var layerGroup = new L.layerGroup().addTo(map);
165
166   function highlight_result(position, bool_focus) {
167     var result = nominatim_results[position];
168     if (!result) { return; }
169     var result_el = get_result_element(position);
170
171     $('.result').removeClass('highlight');
172     result_el.addClass('highlight');
173
174     layerGroup.clearLayers();
175
176     if (result.lat) {
177       var circle = circle_for_result(result);
178       circle.on('click', function () {
179         highlight_result(position);
180       });
181       layerGroup.addLayer(circle);
182     }
183
184     if (result.boundingbox) {
185       var bounds = [
186         [result.boundingbox[0] * 1, result.boundingbox[2] * 1],
187         [result.boundingbox[1] * 1, result.boundingbox[3] * 1]
188       ];
189       map.fitBounds(bounds);
190
191       if (result.geojson && result.geojson.type.match(/(Polygon)|(Line)/)) {
192         //
193         var geojson_layer = L.geoJson(
194           parse_and_normalize_geojson_string(result.geojson),
195           {
196             // https://leafletjs.com/reference-1.0.3.html#path-option
197             style: function (feature) {
198               return { interactive: false, color: 'blue' };
199             }
200           }
201         );
202         layerGroup.addLayer(geojson_layer);
203       }
204       // else {
205       //     var layer = L.rectangle(bounds, {color: "#ff7800", weight: 1} );
206       //     layerGroup.addLayer(layer);
207       // }
208     } else {
209       var result_coord = L.latLng(result.lat, result.lon);
210       if (result_coord) {
211         if (is_reverse_search) {
212           // console.dir([result_coord, [request_lat, request_lon]]);
213           // make sure the search coordinates are in the map view as well
214           map.fitBounds(
215             [result_coord, [request_lat, request_lon]],
216             {
217               padding: [50, 50],
218               maxZoom: map.getZoom()
219             }
220           );
221         } else {
222           map.panTo(result_coord, result.zoom || get_config_value('Map_Default_Zoom'));
223         }
224       }
225     }
226     if (bool_focus) {
227       $('#map').focus();
228     }
229   }
230
231
232   $('.result').on('click', function () {
233     highlight_result($(this).data('position'), true);
234   });
235
236   if (is_reverse_search) {
237     map.on('click', function (e) {
238       $('form input[name=lat]').val(e.latlng.lat);
239       $('form input[name=lon]').val(e.latlng.wrap().lng);
240       $('form').submit();
241     });
242
243     $('#switch-coords').on('click', function (e) {
244       e.preventDefault();
245       e.stopPropagation();
246       var lat = $('form input[name=lat]').val();
247       var lon = $('form input[name=lon]').val();
248       $('form input[name=lat]').val(lon);
249       $('form input[name=lon]').val(lat);
250       $('form').submit();
251     });
252   }
253
254   highlight_result(0, false);
255
256   // common mistake is to copy&paste latitude and longitude into the 'lat' search box
257   $('form input[name=lat]').on('change', function () {
258     var coords = $(this).val().split(',');
259     if (coords.length === 2) {
260       $(this).val(L.Util.trim(coords[0]));
261       $(this).siblings('input[name=lon]').val(L.Util.trim(coords[1]));
262     }
263   });
264 }
265
266
267
268
269
270
271
272 jQuery(document).ready(function () {
273   //
274   if (!$('#search-page,#reverse-page').length) { return; }
275
276   var is_reverse_search = !!($('#reverse-page').length);
277   var endpoint = is_reverse_search ? 'reverse' : 'search';
278
279
280   var search_params = new URLSearchParams(location.search);
281
282   // return view('search', [
283   //     'sQuery' => $sQuery,
284   //     'bAsText' => '',
285   //     'sViewBox' => '',
286   //     'aSearchResults' => $aSearchResults,
287   //     'sMoreURL' => 'example.com',
288   //     'sDataDate' => $this->fetch_status_date(),
289   //     'sApiURL' => $url
290   // ]);
291
292   if (is_reverse_search) {
293     var api_request_params = {
294       lat: search_params.get('lat'),
295       lon: search_params.get('lon'),
296       zoom: (search_params.get('zoom') > 1 ? search_params.get('zoom') : get_config_value('Reverse_Default_Search_Zoom')),
297       format: 'jsonv2'
298     };
299
300     var context = {
301       // aPlace: aPlace,
302       fLat: api_request_params.lat,
303       fLon: api_request_params.lon,
304       iZoom: (search_params.get('zoom') > 1 ? api_request_params.zoom : get_config_value('Reverse_Default_Search_Zoom'))
305     };
306
307     update_html_title();
308     if (api_request_params.lat && api_request_params.lon) {
309
310       fetch_from_api('reverse', api_request_params, function (aPlace) {
311
312         if (aPlace.error) {
313           aPlace = null;
314         }
315
316         context.aPlace = aPlace;
317
318         render_template($('main'), 'reversepage-template', context);
319         update_html_title('Reverse result for ' + api_request_params.lat + ',' + api_request_params.lon);
320
321         init_map_on_search_page(
322           is_reverse_search,
323           [aPlace],
324           api_request_params.lat,
325           api_request_params.lon,
326           api_request_params.zoom
327         );
328
329         update_data_date();
330       });
331     } else {
332       render_template($('main'), 'reversepage-template', context);
333
334       init_map_on_search_page(
335         is_reverse_search,
336         [],
337         get_config_value('Map_Default_Lat'),
338         get_config_value('Map_Default_Lon'),
339         get_config_value('Map_Default_Zoom')
340       );
341     }
342
343   } else {
344     var api_request_params = {
345       q: search_params.get('q'),
346       polygon_geojson: search_params.get('polygon_geojson') ? 1 : 0,
347       viewbox: search_params.get('viewbox'),
348       format: 'jsonv2'
349     };
350
351     var context = {
352       // aSearchResults: aResults,
353       sQuery: api_request_params.q,
354       sViewBox: search_params.get('viewbox'),
355       env: Nominatim_Config,
356       sMoreURL: ''
357     };
358
359     if (api_request_params.q) {
360
361       fetch_from_api('search', api_request_params, function (aResults) {
362
363         context.aSearchResults = aResults;
364
365         render_template($('main'), 'searchpage-template', context);
366         update_html_title('Result for ' + api_request_params.q);
367
368         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'));
369
370         $('#q').focus();
371
372         update_data_date();
373       });
374     } else {
375       render_template($('main'), 'searchpage-template', context);
376
377       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'));
378     }
379   }
380 });