]> git.openstreetmap.org Git - nominatim.git/blob - website/js/nominatim-ui.js
UI: keep map position when switch to reverse. Easy switching of lat,lon to lon,lat
[nominatim.git] / website / js / nominatim-ui.js
1 var map;
2 var last_click_latlng;
3
4 jQuery(document).on('ready', function(){
5
6     if ( !$('#search-page,#reverse-page').length ){ return; }
7     
8     var is_reverse_search = !!( $('#reverse-page').length );
9
10     $('#q').focus();
11
12     map = new L.map('map', {
13                 attributionControl: (nominatim_map_init.tile_attribution && nominatim_map_init.tile_attribution.length),
14                 scrollWheelZoom:    !L.Browser.touch,
15                 touchZoom:          false
16             });
17
18     L.tileLayer(nominatim_map_init.tile_url, {
19         noWrap: true, // otherwise we end up with click coordinates like latitude -728
20         // moved to footer
21         attribution: (nominatim_map_init.tile_attribution || null ) //'&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
22     }).addTo(map);
23
24     map.setView([nominatim_map_init.lat, nominatim_map_init.lon], nominatim_map_init.zoom);
25
26     if ( is_reverse_search ){
27         // We don't need a marker, but an L.circle instance changes radius once you zoom in/out
28         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});
29         cm.addTo(map);
30     }
31
32     var MapPositionControl = L.Control.extend({
33             options: {
34                     position: 'topright'
35             },
36
37             onAdd: function (map) {
38                     var container = L.DomUtil.create('div', 'my-custom-control');
39
40                     $(container).text('show map bounds').addClass('leaflet-bar btn btn-sm btn-default').on('click', function(e){
41                         e.preventDefault();
42                         e.stopPropagation();
43                         $('#map-position').show();
44                         $(container).hide();
45                     });
46                     $('#map-position-close a').on('click', function(e){
47                         e.preventDefault();
48                         e.stopPropagation();
49                         $('#map-position').hide();
50                         $(container).show();
51                     });
52
53                     return container;
54             }
55     });
56
57     map.addControl(new MapPositionControl());
58
59
60     function display_map_position(mouse_lat_lng){
61
62         html_mouse = "mouse position " + (mouse_lat_lng ? [mouse_lat_lng.lat.toFixed(5), mouse_lat_lng.lng.toFixed(5)].join(',') : '-');
63         html_click = "last click: " + (last_click_latlng ? [last_click_latlng.lat.toFixed(5),last_click_latlng.lng.toFixed(5)].join(',') : '-');
64
65         html_center = 
66             "map center: " + 
67             map.getCenter().lat.toFixed(5) + ',' + map.getCenter().lng.toFixed(5) +
68             " <a target='_blank' href='" + map_link_to_osm() + "'>view on osm.org</a>";
69
70         html_zoom = "map zoom: " + map.getZoom();
71
72         html_viewbox = "viewbox: " + map_viewbox_as_string();
73
74         $('#map-position-inner').html([html_center,html_zoom,html_viewbox,html_click,html_mouse].join('<br/>'));
75
76         var reverse_params = {
77             lat: map.getCenter().lat.toFixed(5),
78             lon: map.getCenter().lng.toFixed(5),
79             zoom: map.getZoom(),
80             format: 'html'
81         }
82         $('#switch-to-reverse').attr('href', 'reverse.php?' + $.param(reverse_params));
83
84         $('input#use_viewbox').trigger('change');
85     }
86
87     function update_viewbox_field(){
88         // hidden HTML field
89         $('input[name=viewbox]').val( $('input#use_viewbox').prop('checked') ? map_viewbox_as_string() : '');
90     }
91
92     map.on('move', function(e) {
93         display_map_position();
94         update_viewbox_field();
95     });
96
97     map.on('mousemove', function(e) {
98         display_map_position(e.latlng);
99     });
100
101     map.on('click', function(e) {
102         last_click_latlng = e.latlng;
103         display_map_position();
104     });
105
106     map.on('load', function(e){
107         display_map_position();
108     });
109
110
111     $('input#use_viewbox').on('change', function(){
112         update_viewbox_field();
113     });
114
115
116
117     function map_viewbox_as_string() {
118         // since .toBBoxString() doesn't round numbers
119         return [
120             map.getBounds().getSouthWest().lng.toFixed(5), // left
121             map.getBounds().getNorthEast().lat.toFixed(5), // top
122             map.getBounds().getNorthEast().lng.toFixed(5), // right
123             map.getBounds().getSouthWest().lat.toFixed(5)  // bottom
124         ].join(',');
125     }
126     function map_link_to_osm(){
127         return "http://openstreetmap.org/#map=" + map.getZoom() + "/" + map.getCenter().lat + "/" + map.getCenter().lng;
128     }
129
130     function get_result_element(position){
131         return $('.result').eq(position);
132     }
133     function marker_for_result(result){
134         return L.marker([result.lat,result.lon], {riseOnHover:true,title:result.name });
135     }
136     function circle_for_result(result){
137         return L.circleMarker([result.lat,result.lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75, clickable: !is_reverse_search});
138     }
139
140     var layerGroup = new L.layerGroup().addTo(map);
141     function highlight_result(position, bool_focus){
142         var result = nominatim_results[position];
143         if (!result){ return }
144         var result_el = get_result_element(position);
145
146         $('.result').removeClass('highlight');
147         result_el.addClass('highlight');
148
149         layerGroup.clearLayers();
150
151         if (result.lat){
152             var circle = circle_for_result(result);
153             circle.on('click', function(){
154                 highlight_result(position);
155             });
156             layerGroup.addLayer(circle);            
157         }
158         if (result.aBoundingBox){
159
160             var bounds = [[result.aBoundingBox[0]*1,result.aBoundingBox[2]*1], [result.aBoundingBox[1]*1,result.aBoundingBox[3]*1]];
161             map.fitBounds(bounds);
162             if (result.astext && result.astext.match(/(POLY)|(LINE)/) ){
163                 var geojson_layer = L.geoJson(null, {
164                     // http://leafletjs.com/reference.html#geojson-style
165                     style: function(feature) { return { clickable: false, color: 'blue' }; }
166                 });
167                 omnivore.wkt.parse(result.astext,null,geojson_layer);
168                 layerGroup.addLayer(geojson_layer);
169             }
170             else {
171                 // var layer = L.rectangle(bounds, {color: "#ff7800", weight: 1} );
172                 // layerGroup.addLayer(layer);
173             }
174         }
175         else {
176             if ( is_reverse_search ){
177                 // make sure the search coordinates are in the map view as well
178                 map.fitBounds([[result.lat,result.lon], [nominatim_map_init.lat,nominatim_map_init.lon]], {padding: [50,50], maxZoom: map.getZoom()});
179
180                 // better, but causes a leaflet warning
181                 // map.panInsideBounds([[result.lat,result.lon], [nominatim_map_init.lat,nominatim_map_init.lon]], {animate: false});
182             }
183             else {
184                 map.panTo([result.lat,result.lon], result.zoom || nominatim_map_init.zoom);
185             }
186         }
187
188         // var crosshairIcon = L.icon({
189         //  iconUrl:     'images/crosshair.png',
190         //  iconSize:    [12, 12],
191         //  iconAnchor:  [6, 6],
192         // });
193         // var crossMarker = new L.Marker([result.lat,result.lon], { icon: crosshairIcon, clickable: false});
194         // layerGroup.addLayer(crossMarker);
195
196
197
198         if (bool_focus){
199             $('#map').focus();
200         }
201     }
202
203
204     $('.result').on('click', function(e){
205         highlight_result($(this).data('position'), true);
206     });
207
208     if ( is_reverse_search ){
209         map.on('click', function(e){
210             $('form input[name=lat]').val( e.latlng.lat);
211             $('form input[name=lon]').val( e.latlng.lng);
212             $('form').submit();
213         });
214
215         $('#switch-coords').on('click', function(e){
216             var lat = $('form input[name=lat]').val();
217             var lon = $('form input[name=lon]').val();
218             $('form input[name=lat]').val(lon);
219             $('form input[name=lon]').val(lat);
220             $('form').submit();
221         });
222     }
223
224     highlight_result(0, false);
225
226
227 });
228
229
230 jQuery(document).on('ready', function(){
231
232     if ( !$('#details-page').length ){ return; }
233
234
235         map = new L.map('map', {
236                     // center: [nominatim_map_init.lat, nominatim_map_init.lon],
237                     // zoom:   nominatim_map_init.zoom,
238                     attributionControl: (nominatim_map_init.tile_attribution && nominatim_map_init.tile_attribution.length),
239                     scrollWheelZoom:    false,
240                     touchZoom:          false,
241                 });
242
243
244         L.tileLayer(nominatim_map_init.tile_url, {
245             // moved to footer
246             attribution: (nominatim_map_init.tile_attribution || null ) //'&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
247         }).addTo(map);
248
249
250         var layerGroup = new L.layerGroup().addTo(map);
251
252         var circle = L.circleMarker([nominatim_result.lat,nominatim_result.lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75});
253         map.addLayer(circle);
254
255         if ( nominatim_result.outlinestring ){
256
257             var geojson_layer = L.geoJson(null, {
258                 // http://leafletjs.com/reference.html#geojson-style
259                 style: function(feature) { return { clickable: false, color: 'blue' }; }
260             });
261             omnivore.wkt.parse(nominatim_result.outlinestring,null,geojson_layer);
262             layerGroup.addLayer(geojson_layer);
263             map.fitBounds(geojson_layer.getBounds());
264         } else {
265             map.setView([nominatim_result.lat,nominatim_result.lon],10);
266         }
267
268
269
270 });
271