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