]> git.openstreetmap.org Git - nominatim.git/blob - website/js/nominatim-ui.js
1c90cb0f618074556cc52c73082307acc92ee147
[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.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         } else {
35                 map.setView([0,0],2);
36         }
37
38
39
40         function display_map_position(mouse_lat_lng){
41
42                 html_mouse = "mouse position " + (mouse_lat_lng ? [mouse_lat_lng.lat.toFixed(5), mouse_lat_lng.lng.toFixed(5)].join(',') : '-');
43                 html_click = "last click: " + (last_click_latlng ? [last_click_latlng.lat.toFixed(5),last_click_latlng.lng.toFixed(5)].join(',') : '-');
44
45                 html_center = 
46                         "map center: " + 
47                         map.getCenter().lat.toFixed(5) + ',' + map.getCenter().lng.toFixed(5) +
48                         " <a target='_blank' href='" + map_link_to_osm() + "'>view on osm.org</a>";
49
50                 html_viewbox = "viewbox: " + map_viewbox_as_string();
51
52                 $('#map-position').html([html_center,html_viewbox,html_click,html_mouse].join('<br/>'));
53                 $('input#use_viewbox').trigger('change');
54         }
55
56         map.on('move', function(e) {
57                 display_map_position();
58         });
59
60         map.on('mousemove', function(e) {
61                 display_map_position(e.latlng);
62         });
63
64         map.on('click', function(e) {
65                 last_click_latlng = e.latlng;
66                 display_map_position();
67         });
68
69         map.on('load', function(e){
70                 display_map_position();
71         });
72
73
74         $('input#use_viewbox').on('change', function(){
75                 $('input[name=viewbox]').val( $(this).prop('checked') ? map_viewbox_as_string() : '');
76         });
77
78
79
80         function map_viewbox_as_string() {
81                 // since .toBBoxString() doesn't round numbers
82                 return [
83                         map.getBounds().getSouthWest().lat.toFixed(5),
84                         map.getBounds().getSouthWest().lng.toFixed(5),
85                         map.getBounds().getNorthEast().lat.toFixed(5),
86                         map.getBounds().getNorthEast().lng.toFixed(5) ].join(',');
87         }
88         function map_link_to_osm(){
89                 return "http://openstreetmap.org/#map=" + map.getZoom() + "/" + map.getCenter().lat + "/" + map.getCenter().lng;
90         }
91
92         function get_result_element(position){
93                 return $('.result').eq(position);
94         }
95         function marker_for_result(result){
96                 return L.marker([result.lat,result.lon], {riseOnHover:true,title:result.name });
97         }
98         function circle_for_result(result){
99                 return L.circleMarker([result.lat,result.lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75, clickable: !is_reverse_search});
100         }
101
102         var layerGroup = new L.layerGroup().addTo(map);
103         function highlight_result(position, bool_focus){
104                 var result = nominatim_results[position];
105                 if (!result){ return }
106                 var result_el = get_result_element(position);
107
108                 $('.result').removeClass('highlight');
109                 result_el.addClass('highlight');
110
111                 layerGroup.clearLayers();
112
113                 if (result.lat){
114                         var circle = circle_for_result(result);
115                         circle.on('click', function(){
116                                 highlight_result(position);
117                         });
118                         layerGroup.addLayer(circle);                    
119                 }
120                 if (result.aBoundingBox){
121
122                         var bounds = [[result.aBoundingBox[0]*1,result.aBoundingBox[2]*1], [result.aBoundingBox[1]*1,result.aBoundingBox[3]*1]];
123                         map.fitBounds(bounds);
124                         if (result.astext && result.astext.match(/POLY/) ){
125                                 var layer = omnivore.wkt.parse(result.astext);
126                                 layerGroup.addLayer(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]});
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: false,
189                                         scrollWheelZoom:    false,
190                                         touchZoom:          false,
191                                 });
192
193                 L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
194                         // moved to footer
195                         // attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
196                 }).addTo(map);
197
198                 var layerGroup = new L.layerGroup().addTo(map);
199
200                 var circle = L.circleMarker([nominatim_result.lat,nominatim_result.lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75});
201                 map.addLayer(circle);
202
203                 if ( nominatim_result.outlinestring ){
204                         var outline = omnivore.wkt.parse(nominatim_result.outlinestring);
205                         map.addLayer(outline);
206                         map.fitBounds(outline.getBounds());
207                 } else {
208                         map.setView([nominatim_result.lat,nominatim_result.lon],10);
209                 }
210
211
212
213 });
214