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