1/** 2 * @namespace WPGMZA 3 * @module NominatimGeocoder 4 * @requires WPGMZA.Geocoder 5 */ 6jQuery(function($) { 7 8 /** 9 * @class NominatimGeocoder 10 * @extends Geocoder 11 * @summary OpenLayers geocoder - uses Nominatim by default 12 */ 13 WPGMZA.NominatimGeocoder = function() { 14 15 } 16 17 WPGMZA.NominatimGeocoder.prototype = Object.create(WPGMZA.Geocoder.prototype); 18 WPGMZA.NominatimGeocoder.prototype.constructor = WPGMZA.NominatimGeocoder; 19 20 /** 21 * @function getResponseFromCache 22 * @access protected 23 * @summary Tries to retrieve cached coordinates from server cache 24 * @param {string} address The street address to geocode 25 * @param {function} callback Where to send the results, as an array 26 * @return {void} 27 */ 28 WPGMZA.NominatimGeocoder.prototype.getResponseFromCache = function(query, callback) 29 { 30 WPGMZA.restAPI.call("/geocode-cache", { 31 data: { 32 query: JSON.stringify(query) 33 }, 34 success: function(response, xhr, status) { 35 if(response && response.lon){ 36 // Legacy compatibility support 37 response.lng = response.lon; 38 } 39 40 callback(response); 41 }, 42 useCompressedPathVariable: true 43 }); 44 } 45 46 /** 47 * @function getResponseFromNominatim 48 * @access protected 49 * @summary Queries Nominatim on the specified address 50 * @param {object} options An object containing the options for geocoding, address is a mandatory field 51 * @param {function} callback The function to send the results to, as an array 52 */ 53 WPGMZA.NominatimGeocoder.prototype.getResponseFromNominatim = function(options, callback) 54 { 55 var data = { 56 q: options.address, 57 format: "json" 58 }; 59 60 if(options.componentRestrictions && options.componentRestrictions.country){ 61 data.countrycodes = options.componentRestrictions.country; 62 } else if(options.country){ 63 data.countrycodes = options.country; 64 } 65 66 if(options._query){ 67 data._query = options._query; 68 } 69 70 WPGMZA.restAPI.call("/query-nominatim", { 71 data: { 72 data: data, 73 }, 74 success: function(response, xhr, status) { 75 if(response && response.length){ 76 callback(response); 77 } else { 78 if(response && response.error){ 79 /* There may be an additional error in place */ 80 callback(response.error, WPGMZA.Geocoder.FAIL) 81 } else { 82 callback(null, WPGMZA.Geocoder.FAIL) 83 } 84 } 85 }, 86 error: function(response, xhr, status) { 87 callback(null, WPGMZA.Geocoder.FAIL) 88 } 89 }); 90 } 91 92 /** 93 * @function clearCache 94 * @access protected 95 * @summary Clears the Nomanatim geocode cache 96 * @returns {void} 97 */ 98 WPGMZA.NominatimGeocoder.prototype.clearCache = function(callback) { 99 $.ajax(WPGMZA.ajaxurl, { 100 data: { 101 action: "wpgmza_clear_nominatim_cache", 102 wpgmza_security : WPGMZA.ajaxnonce || false 103 }, 104 method: "POST", 105 success: function(response){ 106 callback(response); 107 } 108 }); 109 } 110 111 WPGMZA.NominatimGeocoder.prototype.getLatLngFromAddress = function(options, callback) { 112 return WPGMZA.NominatimGeocoder.prototype.geocode(options, callback); 113 } 114 115 WPGMZA.NominatimGeocoder.prototype.getAddressFromLatLng = function(options, callback) { 116 return WPGMZA.NominatimGeocoder.prototype.geocode(options, callback); 117 } 118 119 WPGMZA.NominatimGeocoder.prototype.geocode = function(options, callback) { 120 var self = this; 121 122 if(!options) 123 throw new Error("Invalid options"); 124 125 if(WPGMZA.LatLng.REGEXP.test(options.address)) 126 { 127 var latLng = WPGMZA.LatLng.fromString(options.address); 128 129 callback([{ 130 geometry: { 131 location: latLng 132 }, 133 latLng: latLng, 134 lat: latLng.lat, 135 lng: latLng.lng 136 }], WPGMZA.Geocoder.SUCCESS); 137 138 return; 139 } 140 141 if(options.location) 142 options.latLng = new WPGMZA.LatLng(options.location); 143 144 var finish, location; 145 146 if(options.address) { 147 location = options.address; 148 149 finish = function(response, status) 150 { 151 for(var i = 0; i < response.length; i++) 152 { 153 response[i].geometry = { 154 location: new WPGMZA.LatLng({ 155 lat: parseFloat(response[i].lat), 156 lng: parseFloat(response[i].lon) 157 }) 158 }; 159 160 response[i].latLng = { 161 lat: parseFloat(response[i].lat), 162 lng: parseFloat(response[i].lon) 163 }; 164 165 response[i].bounds = new WPGMZA.LatLngBounds( 166 new WPGMZA.LatLng({ 167 lat: response[i].boundingbox[1], 168 lng: response[i].boundingbox[2] 169 }), 170 new WPGMZA.LatLng({ 171 lat: response[i].boundingbox[0], 172 lng: response[i].boundingbox[3] 173 }) 174 ); 175 176 // Backward compatibility with old UGM 177 response[i].lng = response[i].lon; 178 } 179 180 callback(response, status); 181 } 182 } else if(options.latLng) { 183 if(!(options.latLng instanceof WPGMZA.LatLng)){ 184 options.latLng = new WPGMZA.LatLng(options.latLng); 185 } 186 187 location = options.latLng.toString(); 188 189 finish = function(response, status) 190 { 191 var address = response[0].display_name; 192 193 if(options.fullResult){ 194 address = response[0]; 195 } 196 197 callback([address], status); 198 } 199 } else { 200 throw new Error("You must supply either a latLng or address") 201 } 202 203 var query = {location: location, options: options}; 204 this.getResponseFromCache(query, function(response) { 205 if(response.length) { 206 finish(response, WPGMZA.Geocoder.SUCCESS); 207 return; 208 } 209 210 self.getResponseFromNominatim($.extend(options, {address: location, _query : JSON.stringify({location: location, options: options})}), function(response, status) { 211 if(status == WPGMZA.Geocoder.FAIL) { 212 callback(typeof response === 'string' ? response : null, WPGMZA.Geocoder.FAIL); 213 return; 214 } 215 216 if(response.length == 0) { 217 callback([], WPGMZA.Geocoder.ZERO_RESULTS); 218 return; 219 } 220 221 finish(response, WPGMZA.Geocoder.SUCCESS); 222 }); 223 }); 224 } 225});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.