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 update_last_updated(endpoint_name, params) {
100 if (endpoint_name === 'status') return;
102 var api_url = generate_full_api_url(endpoint_name, params);
103 $('#last-updated').show();
105 $('#api-request a').attr('href', api_url);
106 $('#api-request').show();
108 if (endpoint_name === 'search' || endpoint_name === 'reverse') {
109 $('#api-request-debug a').attr('href', api_url + '&debug=1');
110 $('#api-request-debug').show();
112 $('#api-request-debug').hide();
116 function fetch_from_api(endpoint_name, params, callback) {
117 var api_url = generate_full_api_url(endpoint_name, params);
118 $.get(api_url, function (data) {
119 if (endpoint_name !== 'status') {
120 update_last_updated(endpoint_name, params);
126 function update_data_date() {
127 fetch_from_api('status', { format: 'json' }, function (data) {
128 $('#last-updated').show();
129 $('#data-date').text(data.data_updated);
133 function render_template(el, template_name, page_context) {
134 var template_source = $('#' + template_name).text();
135 var template = Handlebars.compile(template_source);
136 var html = template(page_context);
140 function update_html_title(title) {
142 if (title && title.length > 1) {
143 prefix = title + ' | ';
145 $('head title').text(prefix + 'OpenStreetMap Nominatim');
148 function show_error(html) {
149 $('#error-overlay').html(html).show();
152 function hide_error() {
153 $('#error-overlay').empty().hide();
157 jQuery(document).ready(function () {
160 $('#last-updated').hide();
162 $(document).ajaxStart(function () {
163 $('#loading').fadeIn('fast');
164 }).ajaxComplete(function () {
165 $('#loading').fadeOut('fast');
166 }).ajaxError(function (event, jqXHR, ajaxSettings/* , thrownError */) {
167 // console.log(thrownError);
168 // console.log(ajaxSettings);
169 var url = ajaxSettings.url;
170 show_error('Error fetching results from <a href="' + url + '">' + url + '</a>');