]> git.openstreetmap.org Git - nominatim.git/blob - website/js/nominatim-ui.js
Merge pull request #874 from lonvia/check-for-updates
[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     map = new L.map('map', {
29                 attributionControl: (nominatim_map_init.tile_attribution && nominatim_map_init.tile_attribution.length),
30                 scrollWheelZoom:    true, // !L.Browser.touch,
31                 touchZoom:          false
32             });
33
34     L.tileLayer(nominatim_map_init.tile_url, {
35         noWrap: true, // otherwise we end up with click coordinates like latitude -728
36         // moved to footer
37         attribution: (nominatim_map_init.tile_attribution || null ) //'&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
38     }).addTo(map);
39
40     map.setView([nominatim_map_init.lat, nominatim_map_init.lon], nominatim_map_init.zoom);
41
42     var osm2 = new L.TileLayer(nominatim_map_init.tile_url, {minZoom: 0, maxZoom: 13, attribution: (nominatim_map_init.tile_attribution || null )});
43     var miniMap = new L.Control.MiniMap(osm2, {toggleDisplay: true}).addTo(map);
44
45     if ( is_reverse_search ){
46         // We don't need a marker, but an L.circle instance changes radius once you zoom in/out
47         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});
48         cm.addTo(map);
49     }
50
51     var MapPositionControl = L.Control.extend({
52             options: {
53                     position: 'topright'
54             },
55
56             onAdd: function (map) {
57                     var container = L.DomUtil.create('div', 'my-custom-control');
58
59                     $(container).text('show map bounds').addClass('leaflet-bar btn btn-sm btn-default').on('click', function(e){
60                         e.preventDefault();
61                         e.stopPropagation();
62                         $('#map-position').show();
63                         $(container).hide();
64                     });
65                     $('#map-position-close a').on('click', function(e){
66                         e.preventDefault();
67                         e.stopPropagation();
68                         $('#map-position').hide();
69                         $(container).show();
70                     });
71
72                     return container;
73             }
74     });
75
76     map.addControl(new MapPositionControl());
77
78
79     function display_map_position(mouse_lat_lng){
80
81         html_mouse = "mouse position " + (mouse_lat_lng ? [mouse_lat_lng.lat.toFixed(5), mouse_lat_lng.lng.toFixed(5)].join(',') : '-');
82         html_click = "last click: " + (last_click_latlng ? [last_click_latlng.lat.toFixed(5),last_click_latlng.lng.toFixed(5)].join(',') : '-');
83
84         html_center = 
85             "map center: " + 
86             map.getCenter().lat.toFixed(5) + ',' + map.getCenter().lng.toFixed(5) +
87             " <a target='_blank' href='" + map_link_to_osm() + "'>view on osm.org</a>";
88
89         html_zoom = "map zoom: " + map.getZoom();
90
91         html_viewbox = "viewbox: " + map_viewbox_as_string();
92
93         $('#map-position-inner').html([html_center,html_zoom,html_viewbox,html_click,html_mouse].join('<br/>'));
94
95         var reverse_params = {
96             // lat: map.getCenter().lat.toFixed(5),
97             // lon: map.getCenter().lng.toFixed(5),
98             // zoom: 2,
99             format: 'html'
100         }
101         $('#switch-to-reverse').attr('href', 'reverse.php?' + $.param(reverse_params));
102
103         $('input#use_viewbox').trigger('change');
104     }
105
106     function update_viewbox_field(){
107         // hidden HTML field
108         $('input[name=viewbox]').val( $('input#use_viewbox').prop('checked') ? map_viewbox_as_string() : '');
109     }
110
111     map.on('move', function(e) {
112         display_map_position();
113         update_viewbox_field();
114     });
115
116     map.on('mousemove', function(e) {
117         display_map_position(e.latlng);
118     });
119
120     map.on('click', function(e) {
121         last_click_latlng = e.latlng;
122         display_map_position();
123     });
124
125     map.on('load', function(e){
126         display_map_position();
127     });
128
129
130     $('input#use_viewbox').on('change', function(){
131         update_viewbox_field();
132     });
133
134
135
136     function map_viewbox_as_string() {
137         // since .toBBoxString() doesn't round numbers
138         return [
139             map.getBounds().getSouthWest().lng.toFixed(5), // left
140             map.getBounds().getNorthEast().lat.toFixed(5), // top
141             map.getBounds().getNorthEast().lng.toFixed(5), // right
142             map.getBounds().getSouthWest().lat.toFixed(5)  // bottom
143         ].join(',');
144     }
145     function map_link_to_osm(){
146         return "https://openstreetmap.org/#map=" + map.getZoom() + "/" + map.getCenter().lat + "/" + map.getCenter().lng;
147     }
148
149     function get_result_element(position){
150         return $('.result').eq(position);
151     }
152     function marker_for_result(result){
153         return L.marker([result.lat,result.lon], {riseOnHover:true,title:result.name });
154     }
155     function circle_for_result(result){
156         return L.circleMarker([result.lat,result.lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75, clickable: !is_reverse_search});
157     }
158
159     var layerGroup = new L.layerGroup().addTo(map);
160     function highlight_result(position, bool_focus){
161         var result = nominatim_results[position];
162         if (!result){ return }
163         var result_el = get_result_element(position);
164
165         $('.result').removeClass('highlight');
166         result_el.addClass('highlight');
167
168         layerGroup.clearLayers();
169
170         if (result.lat){
171             var circle = circle_for_result(result);
172             circle.on('click', function(){
173                 highlight_result(position);
174             });
175             layerGroup.addLayer(circle);            
176         }
177         if (result.aBoundingBox){
178
179             var bounds = [[result.aBoundingBox[0]*1,result.aBoundingBox[2]*1], [result.aBoundingBox[1]*1,result.aBoundingBox[3]*1]];
180             map.fitBounds(bounds);
181
182             if (result.asgeojson && result.asgeojson.match(/(Polygon)|(Line)/) ){
183
184                 var geojson_layer = L.geoJson(
185                     parse_and_normalize_geojson_string(result.asgeojson),
186                     {
187                         // http://leafletjs.com/reference-1.0.3.html#path-option
188                         style: function(feature) {
189                             return { interactive: false, color: 'blue' }; 
190                         }
191                     }
192                 );
193                 layerGroup.addLayer(geojson_layer);
194             }
195             else {
196                 // var layer = L.rectangle(bounds, {color: "#ff7800", weight: 1} );
197                 // layerGroup.addLayer(layer);
198             }
199         }
200         else {
201             var result_coord = L.latLng(result.lat, result.lon);
202             if ( result_coord ){
203                 if ( is_reverse_search ){
204                     // make sure the search coordinates are in the map view as well
205                     map.fitBounds([result_coord, [nominatim_map_init.lat,nominatim_map_init.lon]], {padding: [50,50], maxZoom: map.getZoom()});
206
207                     // better, but causes a leaflet warning
208                     // map.panInsideBounds([[result.lat,result.lon], [nominatim_map_init.lat,nominatim_map_init.lon]], {animate: false});
209                 }
210                 else {
211                     map.panTo(result_coord, result.zoom || nominatim_map_init.zoom);
212                 }
213             }
214         }
215
216         // var crosshairIcon = L.icon({
217         //  iconUrl:     'images/crosshair.png',
218         //  iconSize:    [12, 12],
219         //  iconAnchor:  [6, 6],
220         // });
221         // var crossMarker = new L.Marker([result.lat,result.lon], { icon: crosshairIcon, clickable: false});
222         // layerGroup.addLayer(crossMarker);
223
224
225
226         if (bool_focus){
227             $('#map').focus();
228         }
229     }
230
231
232     $('.result').on('click', function(e){
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.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 jQuery(document).ready(function(){
269
270     if ( !$('#details-page').length ){ return; }
271
272
273         map = new L.map('map', {
274                     // center: [nominatim_map_init.lat, nominatim_map_init.lon],
275                     // zoom:   nominatim_map_init.zoom,
276                     attributionControl: (nominatim_map_init.tile_attribution && nominatim_map_init.tile_attribution.length),
277                     scrollWheelZoom:    true, // !L.Browser.touch,
278                     touchZoom:          false,
279                 });
280
281
282         L.tileLayer(nominatim_map_init.tile_url, {
283             // moved to footer
284             attribution: (nominatim_map_init.tile_attribution || null ) //'&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
285         }).addTo(map);
286
287         var layerGroup = new L.layerGroup().addTo(map);
288
289         var circle = L.circleMarker([nominatim_result.lat,nominatim_result.lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75});
290         map.addLayer(circle);
291
292         if ( nominatim_result.asgeojson ){
293
294             var geojson_layer = L.geoJson(
295                 parse_and_normalize_geojson_string(nominatim_result.asgeojson),
296                 {
297                     // http://leafletjs.com/reference-1.0.3.html#path-option
298                     style: function(feature) {
299                         return { interactive: false, color: 'blue' }; 
300                     }
301                 }
302             );
303             map.addLayer(geojson_layer);
304             map.fitBounds(geojson_layer.getBounds());
305         } else {
306             map.setView([nominatim_result.lat,nominatim_result.lon],10);
307         }
308
309         var osm2 = new L.TileLayer(nominatim_map_init.tile_url, {minZoom: 0, maxZoom: 13, attribution: (nominatim_map_init.tile_attribution || null )});
310         var miniMap = new L.Control.MiniMap(osm2, {toggleDisplay: true}).addTo(map);
311
312
313 });
314