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 generate_full_api_url(endpoint_name, params) {
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?'
99 function fetch_from_api(endpoint_name, params, callback) {
100 var api_url = generate_full_api_url(endpoint_name, params);
101 if (endpoint_name !== 'status') {
102 $('#api-request-link').attr('href', api_url);
104 $.get(api_url, function (data) {
109 function update_data_date() {
110 fetch_from_api('status', { format: 'json' }, function (data) {
111 $('#data-date').text(data.data_updated);
115 function render_template(el, template_name, page_context) {
116 var template_source = $('#' + template_name).text();
117 var template = Handlebars.compile(template_source);
118 var html = template(page_context);
122 function update_html_title(title) {
124 if (title && title.length > 1) {
125 prefix = title + ' | ';
127 $('head title').text(prefix + 'OpenStreetMap Nominatim');
130 function show_error(html) {
131 $('#error-overlay').html(html).show();
134 function hide_error() {
135 $('#error-overlay').empty().hide();
139 jQuery(document).ready(function () {
142 $(document).ajaxStart(function () {
143 $('#loading').fadeIn('fast');
144 }).ajaxComplete(function () {
145 $('#loading').fadeOut('fast');
146 }).ajaxError(function (event, jqXHR, ajaxSettings/* , thrownError */) {
147 // console.log(thrownError);
148 // console.log(ajaxSettings);
149 var url = ajaxSettings.url;
150 show_error('Error fetching results from <a href="' + url + '">' + url + '</a>');