6 // *********************************************************
8 // *********************************************************
10 var Nominatim_Config_Defaults = {
11 Nominatim_API_Endpoint: 'http://localhost/nominatim/',
12 Images_Base_Url: '/mapicons/',
13 Search_AreaPolygons: 1,
14 Reverse_Default_Search_Zoom: 18,
15 Map_Default_Lat: 20.0,
18 Map_Tile_URL: 'https://{s}.tile.osm.org/{z}/{x}/{y}.png',
19 Map_Tile_Attribution: '<a href="https://osm.org/copyright">OpenStreetMap contributors</a>'
22 // *********************************************************
24 // *********************************************************
27 function get_config_value(str, default_val) {
28 var value = ((typeof Nominatim_Config !== 'undefined')
29 && (typeof Nominatim_Config[str] !== 'undefined'))
30 ? Nominatim_Config[str]
31 : Nominatim_Config_Defaults[str];
32 return (typeof value !== 'undefined' ? value : default_val);
35 function parse_and_normalize_geojson_string(part) {
36 // normalize places the geometry into a featurecollection, similar to
37 // https://github.com/mapbox/geojson-normalize
38 var parsed_geojson = {
39 type: 'FeatureCollection',
48 return parsed_geojson;
51 function map_link_to_osm() {
52 var zoom = map.getZoom();
53 var lat = map.getCenter().lat;
54 var lng = map.getCenter().lng;
55 return 'https://openstreetmap.org/#map=' + zoom + '/' + lat + '/' + lng;
58 function map_viewbox_as_string() {
59 var bounds = map.getBounds();
60 var west = bounds.getWest();
61 var east = bounds.getEast();
63 if ((east - west) >= 360) { // covers more than whole planet
64 west = map.getCenter().lng - 179.999;
65 east = map.getCenter().lng + 179.999;
67 east = L.latLng(77, east).wrap().lng;
68 west = L.latLng(77, west).wrap().lng;
71 west.toFixed(5), // left
72 bounds.getNorth().toFixed(5), // top
73 east.toFixed(5), // right
74 bounds.getSouth().toFixed(5) // bottom
79 // *********************************************************
81 // *********************************************************
83 function fetch_from_api(endpoint_name, params, callback) {
85 // `&a=&b=&c=1` => '&c=1'
86 var param_names = Object.keys(params);
87 for (var i = 0; i < param_names.length; i += 1) {
88 var val = params[param_names[i]];
89 if (typeof (val) === 'undefined' || val === '' || val === null) {
90 delete params[param_names[i]];
94 var api_url = get_config_value('Nominatim_API_Endpoint') + endpoint_name + '.php?'
96 if (endpoint_name !== 'status') {
97 $('#api-request-link').attr('href', api_url);
99 $.get(api_url, function (data) {
104 function update_data_date() {
105 fetch_from_api('status', { format: 'json' }, function (data) {
106 $('#data-date').text(data.data_updated);
110 function render_template(el, template_name, page_context) {
111 var template_source = $('#' + template_name).text();
112 var template = Handlebars.compile(template_source);
113 var html = template(page_context);
117 function update_html_title(title) {
119 if (title && title.length > 1) {
120 prefix = title + ' | ';
122 $('head title').text(prefix + 'OpenStreetMap Nominatim');
125 function show_error(html) {
126 $('#error-overlay').html(html).show();
129 function hide_error() {
130 $('#error-overlay').empty().hide();
134 $(document).ajaxError(function (event, jqXHR, ajaxSettings/* , thrownError */) {
135 // console.log(thrownError);
136 // console.log(ajaxSettings);
137 var url = ajaxSettings.url;
138 show_error('Error fetching results from <a href="' + url + '">' + url + '</a>');
142 jQuery(document).ready(function () {
145 // *********************************************************
147 // *********************************************************
150 function init_map_on_detail_page(lat, lon, geojson) {
151 var attribution = get_config_value('Map_Tile_Attribution') || null;
152 map = new L.map('map', {
153 // center: [nominatim_map_init.lat, nominatim_map_init.lon],
154 // zoom: nominatim_map_init.zoom,
155 attributionControl: (attribution && attribution.length),
156 scrollWheelZoom: true, // !L.Browser.touch,
160 L.tileLayer(get_config_value('Map_Tile_URL'), {
162 // '© <a href="https://osm.org/copyright">OpenStreetMap</a> contributors'
163 attribution: attribution
166 // var layerGroup = new L.layerGroup().addTo(map);
168 var circle = L.circleMarker([lat, lon], {
169 radius: 10, weight: 2, fillColor: '#ff7800', color: 'blue', opacity: 0.75
171 map.addLayer(circle);
174 var geojson_layer = L.geoJson(
175 // https://leafletjs.com/reference-1.0.3.html#path-option
176 parse_and_normalize_geojson_string(geojson),
179 return { interactive: false, color: 'blue' };
183 map.addLayer(geojson_layer);
184 map.fitBounds(geojson_layer.getBounds());
186 map.setView([lat, lon], 10);
189 var osm2 = new L.TileLayer(
190 get_config_value('Map_Tile_URL'),
194 attribution: (get_config_value('Map_Tile_Attribution') || null)
197 (new L.Control.MiniMap(osm2, { toggleDisplay: true })).addTo(map);
201 jQuery(document).ready(function () {
202 if (!$('#details-page').length) { return; }
204 var search_params = new URLSearchParams(window.location.search);
205 // var place_id = search_params.get('place_id');
207 var api_request_params = {
208 place_id: search_params.get('place_id'),
209 osmtype: search_params.get('osmtype'),
210 osmid: search_params.get('osmid'),
211 keywords: search_params.get('keywords'),
213 hierarchy: (search_params.get('hierarchy') === '1' ? 1 : 0),
219 if (api_request_params.place_id || (api_request_params.osmtype && api_request_params.osmid)) {
220 fetch_from_api('details', api_request_params, function (aFeature) {
221 var context = { aPlace: aFeature, base_url: window.location.search };
223 render_template($('main'), 'detailspage-template', context);
224 if (api_request_params.place_id) {
225 update_html_title('Details for ' + api_request_params.place_id);
227 update_html_title('Details for ' + api_request_params.osmtype + api_request_params.osmid);
232 var lat = aFeature.centroid.coordinates[1];
233 var lon = aFeature.centroid.coordinates[0];
234 init_map_on_detail_page(lat, lon, aFeature.geometry);
237 render_template($('main'), 'detailspage-index-template');
240 $('#form-by-type-and-id,#form-by-osm-url').on('submit', function (e) {
243 var val = $(this).find('input[type=edit]').val();
244 var matches = val.match(/^\s*([NWR])(\d+)\s*$/i);
247 matches = val.match(/\/(relation|way|node)\/(\d+)\s*$/);
251 $(this).find('input[name=osmtype]').val(matches[1].charAt(0).toUpperCase());
252 $(this).find('input[name=osmid]').val(matches[2]);
253 $(this).get(0).submit();
255 alert('invalid input');
260 // *********************************************************
261 // FORWARD/REVERSE SEARCH PAGE
262 // *********************************************************
265 function display_map_position(mouse_lat_lng) {
268 mouse_lat_lng = map.wrapLatLng(mouse_lat_lng);
271 var html_mouse = 'mouse position: -';
273 html_mouse = 'mouse position: '
274 + [mouse_lat_lng.lat.toFixed(5), mouse_lat_lng.lng.toFixed(5)].join(',');
276 var html_click = 'last click: -';
277 if (last_click_latlng) {
278 html_click = 'last click: '
279 + [last_click_latlng.lat.toFixed(5), last_click_latlng.lng.toFixed(5)].join(',');
282 var html_center = 'map center: '
283 + map.getCenter().lat.toFixed(5) + ',' + map.getCenter().lng.toFixed(5)
284 + ' <a target="_blank" href="' + map_link_to_osm() + '">view on osm.org</a>';
286 var html_zoom = 'map zoom: ' + map.getZoom();
287 var html_viewbox = 'viewbox: ' + map_viewbox_as_string();
289 $('#map-position-inner').html([
297 var center_lat_lng = map.wrapLatLng(map.getCenter());
298 var reverse_params = {
299 lat: center_lat_lng.lat.toFixed(5),
300 lon: center_lat_lng.lng.toFixed(5)
304 $('#switch-to-reverse').attr('href', 'reverse.html?' + $.param(reverse_params));
306 $('input#use_viewbox').trigger('change');
309 function init_map_on_search_page(is_reverse_search, nominatim_results, request_lat,
310 request_lon, init_zoom) {
312 var attribution = get_config_value('Map_Tile_Attribution') || null;
313 map = new L.map('map', {
314 // center: [nominatim_map_init.lat, nominatim_map_init.lon],
315 // zoom: nominatim_map_init.zoom,
316 attributionControl: (attribution && attribution.length),
317 scrollWheelZoom: true, // !L.Browser.touch,
322 L.tileLayer(get_config_value('Map_Tile_URL'), {
324 // '© <a href="https://osm.org/copyright">OpenStreetMap</a> contributors'
325 attribution: attribution
328 // console.log(Nominatim_Config);
330 map.setView([request_lat, request_lon], init_zoom);
332 var osm2 = new L.TileLayer(get_config_value('Map_Tile_URL'), {
335 attribution: attribution
337 new L.Control.MiniMap(osm2, { toggleDisplay: true }).addTo(map);
339 if (is_reverse_search) {
340 // We don't need a marker, but an L.circle instance changes radius once you zoom in/out
341 var cm = L.circleMarker(
342 [request_lat, request_lon],
346 fillColor: '#ff7800',
355 var search_params = new URLSearchParams(window.location.search);
356 var viewbox = search_params.get('viewbox');
358 var coords = viewbox.split(','); // <x1>,<y1>,<x2>,<y2>
359 var bounds = L.latLngBounds([coords[1], coords[0]], [coords[3], coords[2]]);
360 L.rectangle(bounds, {
370 var MapPositionControl = L.Control.extend({
374 onAdd: function (/* map */) {
375 var container = L.DomUtil.create('div', 'my-custom-control');
377 $(container).text('show map bounds')
378 .addClass('leaflet-bar btn btn-sm btn-outline-secondary')
379 .on('click', function (e) {
382 $('#map-position').show();
385 $('#map-position-close a').on('click', function (e) {
388 $('#map-position').hide();
396 map.addControl(new MapPositionControl());
402 function update_viewbox_field() {
404 $('input[name=viewbox]')
405 .val($('input#use_viewbox')
406 .prop('checked') ? map_viewbox_as_string() : '');
409 map.on('move', function () {
410 display_map_position();
411 update_viewbox_field();
414 map.on('mousemove', function (e) {
415 display_map_position(e.latlng);
418 map.on('click', function (e) {
419 last_click_latlng = e.latlng;
420 display_map_position();
423 map.on('load', function () {
424 display_map_position();
427 $('input#use_viewbox').on('change', function () {
428 update_viewbox_field();
431 function get_result_element(position) {
432 return $('.result').eq(position);
434 // function marker_for_result(result) {
435 // return L.marker([result.lat, result.lon], { riseOnHover: true, title: result.name });
437 function circle_for_result(result) {
441 fillColor: '#ff7800',
444 clickable: !is_reverse_search
446 return L.circleMarker([result.lat, result.lon], cm_style);
449 var layerGroup = (new L.layerGroup()).addTo(map);
451 function highlight_result(position, bool_focus) {
452 var result = nominatim_results[position];
453 if (!result) { return; }
454 var result_el = get_result_element(position);
456 $('.result').removeClass('highlight');
457 result_el.addClass('highlight');
459 layerGroup.clearLayers();
462 var circle = circle_for_result(result);
463 circle.on('click', function () {
464 highlight_result(position);
466 layerGroup.addLayer(circle);
469 if (result.boundingbox) {
471 [result.boundingbox[0] * 1, result.boundingbox[2] * 1],
472 [result.boundingbox[1] * 1, result.boundingbox[3] * 1]
476 if (result.geojson && result.geojson.type.match(/(Polygon)|(Line)/)) {
478 var geojson_layer = L.geoJson(
479 parse_and_normalize_geojson_string(result.geojson),
481 // https://leafletjs.com/reference-1.0.3.html#path-option
482 style: function (/* feature */) {
483 return { interactive: false, color: 'blue' };
487 layerGroup.addLayer(geojson_layer);
490 // var layer = L.rectangle(bounds, {color: "#ff7800", weight: 1} );
491 // layerGroup.addLayer(layer);
494 var result_coord = L.latLng(result.lat, result.lon);
496 if (is_reverse_search) {
497 // console.dir([result_coord, [request_lat, request_lon]]);
498 // make sure the search coordinates are in the map view as well
500 [result_coord, [request_lat, request_lon]],
503 maxZoom: map.getZoom()
507 map.panTo(result_coord, result.zoom || get_config_value('Map_Default_Zoom'));
517 $('.result').on('click', function () {
518 highlight_result($(this).data('position'), true);
521 if (is_reverse_search) {
522 map.on('click', function (e) {
523 $('form input[name=lat]').val(e.latlng.lat);
524 $('form input[name=lon]').val(e.latlng.wrap().lng);
528 $('#switch-coords').on('click', function (e) {
531 var lat = $('form input[name=lat]').val();
532 var lon = $('form input[name=lon]').val();
533 $('form input[name=lat]').val(lon);
534 $('form input[name=lon]').val(lat);
539 highlight_result(0, false);
541 // common mistake is to copy&paste latitude and longitude into the 'lat' search box
542 $('form input[name=lat]').on('change', function () {
543 var coords_split = $(this).val().split(',');
544 if (coords_split.length === 2) {
545 $(this).val(L.Util.trim(coords_split[0]));
546 $(this).siblings('input[name=lon]').val(L.Util.trim(coords_split[1]));
554 jQuery(document).ready(function () {
556 if (!$('#search-page,#reverse-page').length) { return; }
558 var is_reverse_search = !!($('#reverse-page').length);
560 var search_params = new URLSearchParams(window.location.search);
562 // return view('search', [
563 // 'sQuery' => $sQuery,
566 // 'aSearchResults' => $aSearchResults,
567 // 'sMoreURL' => 'example.com',
568 // 'sDataDate' => $this->fetch_status_date(),
572 var api_request_params;
575 if (is_reverse_search) {
576 api_request_params = {
577 lat: search_params.get('lat'),
578 lon: search_params.get('lon'),
579 zoom: (search_params.get('zoom') > 1
580 ? search_params.get('zoom')
581 : get_config_value('Reverse_Default_Search_Zoom')),
587 fLat: api_request_params.lat,
588 fLon: api_request_params.lon,
589 iZoom: (search_params.get('zoom') > 1
590 ? api_request_params.zoom
591 : get_config_value('Reverse_Default_Search_Zoom'))
595 if (api_request_params.lat && api_request_params.lon) {
597 fetch_from_api('reverse', api_request_params, function (aPlace) {
603 context.aPlace = aPlace;
605 render_template($('main'), 'reversepage-template', context);
606 update_html_title('Reverse result for '
607 + api_request_params.lat
609 + api_request_params.lon);
611 init_map_on_search_page(
614 api_request_params.lat,
615 api_request_params.lon,
616 api_request_params.zoom
622 render_template($('main'), 'reversepage-template', context);
624 init_map_on_search_page(
627 get_config_value('Map_Default_Lat'),
628 get_config_value('Map_Default_Lon'),
629 get_config_value('Map_Default_Zoom')
634 api_request_params = {
635 q: search_params.get('q'),
636 street: search_params.get('street'),
637 city: search_params.get('city'),
638 county: search_params.get('county'),
639 state: search_params.get('state'),
640 country: search_params.get('country'),
641 postalcode: search_params.get('postalcode'),
642 polygon_geojson: get_config_value('Search_AreaPolygons', false) ? 1 : 0,
643 viewbox: search_params.get('viewbox'),
644 exclude_place_ids: search_params.get('exclude_place_ids'),
649 sQuery: api_request_params.q,
650 sViewBox: search_params.get('viewbox'),
654 if (api_request_params.street || api_request_params.city || api_request_params.county
655 || api_request_params.state || api_request_params.country || api_request_params.postalcode) {
656 context.hStructured = {
657 street: api_request_params.street,
658 city: api_request_params.city,
659 county: api_request_params.county,
660 state: api_request_params.state,
661 country: api_request_params.country,
662 postalcode: api_request_params.postalcode
666 if (api_request_params.q || context.hStructured) {
668 fetch_from_api('search', api_request_params, function (aResults) {
670 context.aSearchResults = aResults;
672 if (aResults.length >= 10) {
673 var aExcludePlaceIds = [];
674 if (search_params.has('exclude_place_ids')) {
675 aExcludePlaceIds.search_params.get('exclude_place_ids').split(',');
677 for (var i = 0; i < aResults.length; i += 1) {
678 aExcludePlaceIds.push(aResults[i].place_id);
681 var parsed_url = new URLSearchParams(window.location.search);
682 parsed_url.set('exclude_place_ids', aExcludePlaceIds.join(','));
683 context.sMoreURL = '?' + parsed_url.toString();
686 render_template($('main'), 'searchpage-template', context);
687 update_html_title('Result for ' + api_request_params.q);
689 init_map_on_search_page(
692 get_config_value('Map_Default_Lat'),
693 get_config_value('Map_Default_Lon'),
694 get_config_value('Map_Default_Zoom')
702 render_template($('main'), 'searchpage-template', context);
704 init_map_on_search_page(
707 get_config_value('Map_Default_Lat'),
708 get_config_value('Map_Default_Lon'),
709 get_config_value('Map_Default_Zoom')
714 // *********************************************************
716 // *********************************************************
718 jQuery(document).ready(function () {
719 if (!$('#deletable-page').length) { return; }
721 var api_request_params = {
725 fetch_from_api('deletable', api_request_params, function (aPolygons) {
726 var context = { aPolygons: aPolygons };
728 render_template($('main'), 'deletable-template', context);
729 update_html_title('Deletable objects');
734 // *********************************************************
735 // BROKEN POLYGON PAGE
736 // *********************************************************
738 jQuery(document).ready(function () {
739 if (!$('#polygons-page').length) { return; }
741 var api_request_params = {
745 fetch_from_api('polygons', api_request_params, function (aPolygons) {
746 var context = { aPolygons: aPolygons };
748 render_template($('main'), 'polygons-template', context);
749 update_html_title('Broken polygons');