]> git.openstreetmap.org Git - nominatim.git/blob - website/js/nominatim-ui.js
7c19b17a98a82cbfbbc9da2c85e79497d16b31dc
[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         $('input#use_viewbox').trigger('change');
76     }
77
78     function update_viewbox_field(){
79         // hidden HTML field
80         $('input[name=viewbox]').val( $('input#use_viewbox').prop('checked') ? map_viewbox_as_string() : '');
81     }
82
83     map.on('move', function(e) {
84         display_map_position();
85         update_viewbox_field();
86     });
87
88     map.on('mousemove', function(e) {
89         display_map_position(e.latlng);
90     });
91
92     map.on('click', function(e) {
93         last_click_latlng = e.latlng;
94         display_map_position();
95     });
96
97     map.on('load', function(e){
98         display_map_position();
99     });
100
101
102     $('input#use_viewbox').on('change', function(){
103         update_viewbox_field();
104     });
105
106
107
108     function map_viewbox_as_string() {
109         // since .toBBoxString() doesn't round numbers
110         return [
111             map.getBounds().getSouthWest().lng.toFixed(5), // left
112             map.getBounds().getNorthEast().lat.toFixed(5), // top
113             map.getBounds().getNorthEast().lng.toFixed(5), // right
114             map.getBounds().getSouthWest().lat.toFixed(5)  // bottom
115         ].join(',');
116     }
117     function map_link_to_osm(){
118         return "http://openstreetmap.org/#map=" + map.getZoom() + "/" + map.getCenter().lat + "/" + map.getCenter().lng;
119     }
120
121     function get_result_element(position){
122         return $('.result').eq(position);
123     }
124     function marker_for_result(result){
125         return L.marker([result.lat,result.lon], {riseOnHover:true,title:result.name });
126     }
127     function circle_for_result(result){
128         return L.circleMarker([result.lat,result.lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75, clickable: !is_reverse_search});
129     }
130
131     var layerGroup = new L.layerGroup().addTo(map);
132     function highlight_result(position, bool_focus){
133         var result = nominatim_results[position];
134         if (!result){ return }
135         var result_el = get_result_element(position);
136
137         $('.result').removeClass('highlight');
138         result_el.addClass('highlight');
139
140         layerGroup.clearLayers();
141
142         if (result.lat){
143             var circle = circle_for_result(result);
144             circle.on('click', function(){
145                 highlight_result(position);
146             });
147             layerGroup.addLayer(circle);            
148         }
149         if (result.aBoundingBox){
150
151             var bounds = [[result.aBoundingBox[0]*1,result.aBoundingBox[2]*1], [result.aBoundingBox[1]*1,result.aBoundingBox[3]*1]];
152             map.fitBounds(bounds);
153             if (result.astext && result.astext.match(/(POLY)|(LINE)/) ){
154                 var geojson_layer = L.geoJson(null, {
155                     // http://leafletjs.com/reference.html#geojson-style
156                     style: function(feature) { return { clickable: false, color: 'blue' }; }
157                 });
158                 omnivore.wkt.parse(result.astext,null,geojson_layer);
159                 layerGroup.addLayer(geojson_layer);
160             }
161             else {
162                 // var layer = L.rectangle(bounds, {color: "#ff7800", weight: 1} );
163                 // layerGroup.addLayer(layer);
164             }
165         }
166         else {
167             if ( is_reverse_search ){
168                 // make sure the search coordinates are in the map view as well
169                 map.fitBounds([[result.lat,result.lon], [nominatim_map_init.lat,nominatim_map_init.lon]], {padding: [50,50], maxZoom: map.getZoom()});
170
171                 // better, but causes a leaflet warning
172                 // map.panInsideBounds([[result.lat,result.lon], [nominatim_map_init.lat,nominatim_map_init.lon]], {animate: false});
173             }
174             else {
175                 map.panTo([result.lat,result.lon], result.zoom || nominatim_map_init.zoom);
176             }
177         }
178
179         // var crosshairIcon = L.icon({
180         //  iconUrl:     'images/crosshair.png',
181         //  iconSize:    [12, 12],
182         //  iconAnchor:  [6, 6],
183         // });
184         // var crossMarker = new L.Marker([result.lat,result.lon], { icon: crosshairIcon, clickable: false});
185         // layerGroup.addLayer(crossMarker);
186
187
188
189         if (bool_focus){
190             $('#map').focus();
191         }
192     }
193
194
195     $('.result').on('click', function(e){
196         highlight_result($(this).data('position'), true);
197     });
198
199     if ( is_reverse_search ){
200         map.on('click', function(e){
201             $('form input[name=lat]').val( e.latlng.lat);
202             $('form input[name=lon]').val( e.latlng.lng);
203             $('form').submit();
204         });
205     }
206
207     highlight_result(0, false);
208
209
210 });
211
212
213 jQuery(document).on('ready', function(){
214
215     if ( !$('#details-page').length ){ return; }
216
217
218         map = new L.map('map', {
219                     // center: [nominatim_map_init.lat, nominatim_map_init.lon],
220                     // zoom:   nominatim_map_init.zoom,
221                     attributionControl: (nominatim_map_init.tile_attribution && nominatim_map_init.tile_attribution.length),
222                     scrollWheelZoom:    false,
223                     touchZoom:          false,
224                 });
225
226
227         L.tileLayer(nominatim_map_init.tile_url, {
228             // moved to footer
229             attribution: (nominatim_map_init.tile_attribution || null ) //'&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
230         }).addTo(map);
231
232
233         var layerGroup = new L.layerGroup().addTo(map);
234
235         var circle = L.circleMarker([nominatim_result.lat,nominatim_result.lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75});
236         map.addLayer(circle);
237
238         if ( nominatim_result.outlinestring ){
239
240             var geojson_layer = L.geoJson(null, {
241                 // http://leafletjs.com/reference.html#geojson-style
242                 style: function(feature) { return { clickable: false, color: 'blue' }; }
243             });
244             omnivore.wkt.parse(nominatim_result.outlinestring,null,geojson_layer);
245             layerGroup.addLayer(geojson_layer);
246             map.fitBounds(geojson_layer.getBounds());
247         } else {
248             map.setView([nominatim_result.lat,nominatim_result.lon],10);
249         }
250
251
252
253 });
254