]> git.openstreetmap.org Git - nominatim.git/blob - website/js/nominatim-ui.js
user interface: reverse search remembers zoom level after map click
[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: false, // moved to page footer
14                                 scrollWheelZoom:    !L.Browser.touch,
15                                 touchZoom:          false
16                         });
17
18
19
20         L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
21                 noWrap: true // otherwise we end up with click coordinates like latitude -728
22                 // moved to footer
23                 // attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
24         }).addTo(map);
25
26         if ( nominatim_map_init.lat ){
27                 map.setView([nominatim_map_init.lat || 0, nominatim_map_init.lon], (nominatim_map_init.prevmapzoom || nominatim_map_init.zoom) );
28
29                 if ( is_reverse_search ){
30                         // not really a market, but the .circle changes radius once you zoom in/out
31                         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});
32                         cm.addTo(map);
33                 }
34
35         } else {
36                 map.setView([0,0],2);
37         }
38
39
40
41         function display_map_position(mouse_lat_lng){
42
43                 html_mouse = "mouse position " + (mouse_lat_lng ? [mouse_lat_lng.lat.toFixed(5), mouse_lat_lng.lng.toFixed(5)].join(',') : '-');
44                 html_click = "last click: " + (last_click_latlng ? [last_click_latlng.lat.toFixed(5),last_click_latlng.lng.toFixed(5)].join(',') : '-');
45
46                 html_center = 
47                         "map center: " + 
48                         map.getCenter().lat.toFixed(5) + ',' + map.getCenter().lng.toFixed(5) +
49                         " <a target='_blank' href='" + map_link_to_osm() + "'>view on osm.org</a>";
50
51                 html_viewbox = "viewbox: " + map_viewbox_as_string();
52
53                 html_zoom = "zoom: " + map.getZoom();
54
55                 $('#map-position').html([html_center,html_zoom,html_viewbox,html_click,html_mouse].join('<br/>'));
56                 $('input#use_viewbox').trigger('change');
57         }
58
59         map.on('move', function(e) {
60                 display_map_position();
61         });
62
63         map.on('mousemove', function(e) {
64                 display_map_position(e.latlng);
65         });
66
67         map.on('click', function(e) {
68                 last_click_latlng = e.latlng;
69                 display_map_position();
70         });
71
72         map.on('load', function(e){
73                 display_map_position();
74         });
75
76
77         $('input#use_viewbox').on('change', function(){
78                 $('input[name=viewbox]').val( $(this).prop('checked') ? map_viewbox_as_string() : '');
79         });
80
81
82
83         function map_viewbox_as_string() {
84                 // since .toBBoxString() doesn't round numbers
85                 return [
86                         map.getBounds().getSouthWest().lat.toFixed(5),
87                         map.getBounds().getSouthWest().lng.toFixed(5),
88                         map.getBounds().getNorthEast().lat.toFixed(5),
89                         map.getBounds().getNorthEast().lng.toFixed(5) ].join(',');
90         }
91         function map_link_to_osm(){
92                 return "http://openstreetmap.org/#map=" + map.getZoom() + "/" + map.getCenter().lat + "/" + map.getCenter().lng;
93         }
94
95         function get_result_element(position){
96                 return $('.result').eq(position);
97         }
98         function marker_for_result(result){
99                 return L.marker([result.lat,result.lon], {riseOnHover:true,title:result.name });
100         }
101         function circle_for_result(result){
102                 return L.circleMarker([result.lat,result.lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75, clickable: !is_reverse_search});
103         }
104
105         var layerGroup = new L.layerGroup().addTo(map);
106         function highlight_result(position, bool_focus){
107                 var result = nominatim_results[position];
108                 if (!result){ return }
109                 var result_el = get_result_element(position);
110
111                 $('.result').removeClass('highlight');
112                 result_el.addClass('highlight');
113
114                 layerGroup.clearLayers();
115
116                 if (result.lat){
117                         var circle = circle_for_result(result);
118                         circle.on('click', function(){
119                                 highlight_result(position);
120                         });
121                         layerGroup.addLayer(circle);                    
122                 }
123                 if (result.aBoundingBox){
124
125                         var bounds = [[result.aBoundingBox[0]*1,result.aBoundingBox[2]*1], [result.aBoundingBox[1]*1,result.aBoundingBox[3]*1]];
126                         map.fitBounds(bounds);
127                         if (result.astext && result.astext.match(/POLY/) ){
128                                 var layer = omnivore.wkt.parse(result.astext);
129                                 layerGroup.addLayer(layer);
130                         }
131                         else {
132                                 // var layer = L.rectangle(bounds, {color: "#ff7800", weight: 1} );
133                                 // layerGroup.addLayer(layer);
134                         }
135                 }
136                 else {
137                         if ( is_reverse_search ){
138                                 // make sure the search coordinates are in the map view as well
139                                 map.fitBounds([[result.lat,result.lon], [nominatim_map_init.lat,nominatim_map_init.lon]], {padding: [50,50], maxZoom: map.getZoom()});
140
141                                 // better, but causes a leaflet warning
142                                 // map.panInsideBounds([[result.lat,result.lon], [nominatim_map_init.lat,nominatim_map_init.lon]], {animate: false});
143                         }
144                         else {
145                                 map.panTo([result.lat,result.lon], result.zoom || nominatim_map_init.zoom);
146                         }
147                 }
148
149                 // var crosshairIcon = L.icon({
150                 //      iconUrl:     'images/crosshair.png',
151                 //      iconSize:    [12, 12],
152                 //      iconAnchor:  [6, 6],
153                 // });
154                 // var crossMarker = new L.Marker([result.lat,result.lon], { icon: crosshairIcon, clickable: false});
155                 // layerGroup.addLayer(crossMarker);
156
157
158
159                 if (bool_focus){
160                         $('#map').focus();
161                 }
162         }
163
164
165         $('.result').on('click', function(e){
166                 highlight_result($(this).data('position'), true);
167         });
168
169         if ( is_reverse_search ){
170                 map.on('click', function(e){
171                         $('form input[name=lat]').val( e.latlng.lat);
172                         $('form input[name=lon]').val( e.latlng.lng);
173                         if ( map.getZoom() > 2 ){ $('form input[name=prevmapzoom]').val( map.getZoom() ); }
174                         $('form').submit();
175                 });
176         }
177
178         highlight_result(0, false);
179
180
181 });
182
183
184 jQuery(document).on('ready', function(){
185
186         if ( !$('#details-page').length ){ return; }
187
188
189                 map = new L.map('map', {
190                                         // center: [nominatim_map_init.lat, nominatim_map_init.lon],
191                                         // zoom:   nominatim_map_init.zoom,
192                                         attributionControl: false,
193                                         scrollWheelZoom:    false,
194                                         touchZoom:          false,
195                                 });
196
197                 L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
198                         // moved to footer
199                         // attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
200                 }).addTo(map);
201
202                 var layerGroup = new L.layerGroup().addTo(map);
203
204                 var circle = L.circleMarker([nominatim_result.lat,nominatim_result.lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75});
205                 map.addLayer(circle);
206
207                 if ( nominatim_result.outlinestring ){
208                         var outline = omnivore.wkt.parse(nominatim_result.outlinestring);
209                         map.addLayer(outline);
210                         map.fitBounds(outline.getBounds());
211                 } else {
212                         map.setView([nominatim_result.lat,nominatim_result.lon],10);
213                 }
214
215
216
217 });
218