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