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
8 type: "FeatureCollection",
12 geometry: JSON.parse(raw_string),
17 return parsed_geojson;
20 jQuery(document).ready(function(){
22 if ( !$('#search-page,#reverse-page').length ){ return; }
24 var is_reverse_search = !!( $('#reverse-page').length );
28 $(document).ready(function() {
29 $("input[name='query-selector']").click(function(){
30 var query_val = $("input[name='query-selector']:checked").val() ;
31 if (query_val == "simple") {
32 $("div.form-group-structured").hide();
33 $("div.form-group-simple").show();
34 $('.form-group-structured').find('input:text').val('');
36 else if (query_val == "structured") {
37 $("div.form-group-simple").hide();
38 $("div.form-group-structured").show();
39 $('.form-group-simple').find('input:text').val('');
44 map = new L.map('map', {
45 attributionControl: (nominatim_map_init.tile_attribution && nominatim_map_init.tile_attribution.length),
46 scrollWheelZoom: true, // !L.Browser.touch,
50 L.tileLayer(nominatim_map_init.tile_url, {
52 attribution: (nominatim_map_init.tile_attribution || null ) //'© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
55 map.setView([nominatim_map_init.lat, nominatim_map_init.lon], nominatim_map_init.zoom);
57 var osm2 = new L.TileLayer(nominatim_map_init.tile_url, {minZoom: 0, maxZoom: 13, attribution: (nominatim_map_init.tile_attribution || null )});
58 var miniMap = new L.Control.MiniMap(osm2, {toggleDisplay: true}).addTo(map);
60 if ( is_reverse_search ){
61 // We don't need a marker, but an L.circle instance changes radius once you zoom in/out
62 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});
66 var MapPositionControl = L.Control.extend({
71 onAdd: function (map) {
72 var container = L.DomUtil.create('div', 'my-custom-control');
74 $(container).text('show map bounds').addClass('leaflet-bar btn btn-sm btn-default').on('click', function(e){
77 $('#map-position').show();
80 $('#map-position-close a').on('click', function(e){
83 $('#map-position').hide();
91 map.addControl(new MapPositionControl());
94 function display_map_position(mouse_lat_lng){
97 mouse_lat_lng = map.wrapLatLng(mouse_lat_lng);
99 html_mouse = "mouse position " + (mouse_lat_lng ? [mouse_lat_lng.lat.toFixed(5), mouse_lat_lng.lng.toFixed(5)].join(',') : '-');
100 html_click = "last click: " + (last_click_latlng ? [last_click_latlng.lat.toFixed(5),last_click_latlng.lng.toFixed(5)].join(',') : '-');
104 map.getCenter().lat.toFixed(5) + ',' + map.getCenter().lng.toFixed(5) +
105 " <a target='_blank' href='" + map_link_to_osm() + "'>view on osm.org</a>";
107 html_zoom = "map zoom: " + map.getZoom();
109 html_viewbox = "viewbox: " + map_viewbox_as_string();
111 $('#map-position-inner').html([html_center,html_zoom,html_viewbox,html_click,html_mouse].join('<br/>'));
113 var center_lat_lng = map.wrapLatLng(map.getCenter());
114 var reverse_params = {
115 lat: center_lat_lng.lat.toFixed(5),
116 lon: center_lat_lng.lng.toFixed(5),
120 $('#switch-to-reverse').attr('href', 'reverse.php?' + $.param(reverse_params));
122 $('input#use_viewbox').trigger('change');
125 function update_viewbox_field(){
127 $('input[name=viewbox]').val( $('input#use_viewbox').prop('checked') ? map_viewbox_as_string() : '');
130 map.on('move', function(e) {
131 display_map_position();
132 update_viewbox_field();
135 map.on('mousemove', function(e) {
136 display_map_position(e.latlng);
139 map.on('click', function(e) {
140 last_click_latlng = e.latlng;
141 display_map_position();
144 map.on('load', function(e){
145 display_map_position();
149 $('input#use_viewbox').on('change', function(){
150 update_viewbox_field();
155 function map_viewbox_as_string() {
156 var bounds = map.getBounds();
157 var west = bounds.getWest();
158 var east = bounds.getEast();
160 if ((east - west) >= 360) { // covers more than whole planet
161 west = map.getCenter().lng-179.999;
162 east = map.getCenter().lng+179.999;
164 east = L.latLng(77, east).wrap().lng;
165 west = L.latLng(77, west).wrap().lng;
168 west.toFixed(5), // left
169 bounds.getNorth().toFixed(5), // top
170 east.toFixed(5), // right
171 bounds.getSouth().toFixed(5) // bottom
174 function map_link_to_osm(){
175 return "https://openstreetmap.org/#map=" + map.getZoom() + "/" + map.getCenter().lat + "/" + map.getCenter().lng;
178 function get_result_element(position){
179 return $('.result').eq(position);
181 function marker_for_result(result){
182 return L.marker([result.lat,result.lon], {riseOnHover:true,title:result.name });
184 function circle_for_result(result){
185 return L.circleMarker([result.lat,result.lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75, clickable: !is_reverse_search});
188 var layerGroup = new L.layerGroup().addTo(map);
189 function highlight_result(position, bool_focus){
190 var result = nominatim_results[position];
191 if (!result){ return }
192 var result_el = get_result_element(position);
194 $('.result').removeClass('highlight');
195 result_el.addClass('highlight');
197 layerGroup.clearLayers();
200 var circle = circle_for_result(result);
201 circle.on('click', function(){
202 highlight_result(position);
204 layerGroup.addLayer(circle);
206 if (result.aBoundingBox){
208 var bounds = [[result.aBoundingBox[0]*1,result.aBoundingBox[2]*1], [result.aBoundingBox[1]*1,result.aBoundingBox[3]*1]];
209 map.fitBounds(bounds);
211 if (result.asgeojson && result.asgeojson.match(/(Polygon)|(Line)/) ){
213 var geojson_layer = L.geoJson(
214 parse_and_normalize_geojson_string(result.asgeojson),
216 // http://leafletjs.com/reference-1.0.3.html#path-option
217 style: function(feature) {
218 return { interactive: false, color: 'blue' };
222 layerGroup.addLayer(geojson_layer);
225 // var layer = L.rectangle(bounds, {color: "#ff7800", weight: 1} );
226 // layerGroup.addLayer(layer);
230 var result_coord = L.latLng(result.lat, result.lon);
232 if ( is_reverse_search ){
233 // make sure the search coordinates are in the map view as well
234 map.fitBounds([result_coord, [nominatim_map_init.lat,nominatim_map_init.lon]], {padding: [50,50], maxZoom: map.getZoom()});
236 // better, but causes a leaflet warning
237 // map.panInsideBounds([[result.lat,result.lon], [nominatim_map_init.lat,nominatim_map_init.lon]], {animate: false});
240 map.panTo(result_coord, result.zoom || nominatim_map_init.zoom);
245 // var crosshairIcon = L.icon({
246 // iconUrl: 'images/crosshair.png',
247 // iconSize: [12, 12],
248 // iconAnchor: [6, 6],
250 // var crossMarker = new L.Marker([result.lat,result.lon], { icon: crosshairIcon, clickable: false});
251 // layerGroup.addLayer(crossMarker);
261 $('.result').on('click', function(e){
262 highlight_result($(this).data('position'), true);
265 if ( is_reverse_search ){
266 map.on('click', function(e){
267 $('form input[name=lat]').val( e.latlng.lat);
268 $('form input[name=lon]').val( e.latlng.wrap().lng);
272 $('#switch-coords').on('click', function(e){
275 var lat = $('form input[name=lat]').val();
276 var lon = $('form input[name=lon]').val();
277 $('form input[name=lat]').val(lon);
278 $('form input[name=lon]').val(lat);
282 var search_params = new URLSearchParams(location.search);
283 var viewbox = search_params.get('viewbox');
285 var coords = viewbox.split(','); // <x1>,<y1>,<x2>,<y2>
286 var bounds = L.latLngBounds([coords[1], coords[0]], [coords[3], coords[2]]);
287 L.rectangle(bounds, {color: "#69d53e", weight: 3, dashArray: '5 5', opacity: 0.8, fill: false}).addTo(map);
291 highlight_result(0, false);
293 // common mistake is to copy&paste latitude and longitude into the 'lat' search box
294 $('form input[name=lat]').on('change', function(){
295 var coords = $(this).val().split(',');
296 if (coords.length == 2) {
297 $(this).val(L.Util.trim(coords[0]));
298 $(this).siblings('input[name=lon]').val(L.Util.trim(coords[1]));
305 jQuery(document).ready(function(){
307 if ( !$('#details-index-page').length ){ return; }
309 $('#form-by-type-and-id,#form-by-osm-url').on('submit', function(e){
312 var val = $(this).find('input[type=edit]').val();
313 var matches = val.match(/^\s*([NWR])(\d+)\s*$/i);
316 matches = val.match(/\/(relation|way|node)\/(\d+)\s*$/);
320 $(this).find('input[name=osmtype]').val(matches[1].charAt(0).toUpperCase());
321 $(this).find('input[name=osmid]').val(matches[2]);
322 $(this).get(0).submit();
324 alert('invalid input');
329 jQuery(document).ready(function(){
331 if ( !$('#details-page').length ){ return; }
334 map = new L.map('map', {
335 // center: [nominatim_map_init.lat, nominatim_map_init.lon],
336 // zoom: nominatim_map_init.zoom,
337 attributionControl: (nominatim_map_init.tile_attribution && nominatim_map_init.tile_attribution.length),
338 scrollWheelZoom: true, // !L.Browser.touch,
343 L.tileLayer(nominatim_map_init.tile_url, {
345 attribution: (nominatim_map_init.tile_attribution || null ) //'© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
348 var layerGroup = new L.layerGroup().addTo(map);
350 var circle = L.circleMarker([nominatim_result.lat,nominatim_result.lon], { radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75});
351 map.addLayer(circle);
353 if ( nominatim_result.asgeojson ){
355 var geojson_layer = L.geoJson(
356 parse_and_normalize_geojson_string(nominatim_result.asgeojson),
358 // http://leafletjs.com/reference-1.0.3.html#path-option
359 style: function(feature) {
360 return { interactive: false, color: 'blue' };
364 map.addLayer(geojson_layer);
365 map.fitBounds(geojson_layer.getBounds());
367 map.setView([nominatim_result.lat,nominatim_result.lon],10);
370 var osm2 = new L.TileLayer(nominatim_map_init.tile_url, {minZoom: 0, maxZoom: 13, attribution: (nominatim_map_init.tile_attribution || null )});
371 var miniMap = new L.Control.MiniMap(osm2, {toggleDisplay: true}).addTo(map);