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