1/** 2 * @namespace WPGMZA 3 * @module Geocoder 4 * @requires WPGMZA 5 */ 6jQuery(function($) { 7 8 /** 9 * Base class for geocoders. <strong>Please <em>do not</em> call this constructor directly. Always use createInstance rather than instantiating this class directly.</strong> Using createInstance allows this class to be externally extensible. 10 * @class WPGMZA.Geocoder 11 * @constructor WPGMZA.Geocoder 12 * @memberof WPGMZA 13 * @see WPGMZA.Geocoder.createInstance 14 */ 15 WPGMZA.Geocoder = function() 16 { 17 WPGMZA.assertInstanceOf(this, "Geocoder"); 18 } 19 20 /** 21 * Indicates a successful geocode, with one or more results 22 * @constant SUCCESS 23 * @memberof WPGMZA.Geocoder 24 */ 25 WPGMZA.Geocoder.SUCCESS = "success"; 26 27 /** 28 * Indicates the geocode was successful, but returned no results 29 * @constant ZERO_RESULTS 30 * @memberof WPGMZA.Geocoder 31 */ 32 WPGMZA.Geocoder.ZERO_RESULTS = "zero-results"; 33 34 /** 35 * Indicates the geocode failed, usually due to technical reasons (eg connectivity) 36 * @constant FAIL 37 * @memberof WPGMZA.Geocoder 38 */ 39 WPGMZA.Geocoder.FAIL = "fail"; 40 41 WPGMZA.Geocoder.Providers = { 42 GOOGLE_MAPS : 1, 43 NOMINATIM : 2, 44 OPEN_LAYERS : 3, 45 LEAFLET : 4, 46 AZURE_MAPS : 5, 47 LOCATION_IQ : 6 48 }; 49 50 /** 51 * Returns the contructor to be used by createInstance, depending on the selected maps engine. 52 * @method 53 * @memberof WPGMZA.Geocoder 54 * @return {function} The appropriate contructor 55 */ 56 WPGMZA.Geocoder.getConstructor = function() { 57 let provider = WPGMZA.settings && WPGMZA.settings.address_provider ? WPGMZA.settings.address_provider : false; 58 if(!provider){ 59 /* Provider unknown, use engine default */ 60 if(WPGMZA.settings && WPGMZA.settings.engine){ 61 switch(WPGMZA.settings.engine){ 62 case 'google-maps': 63 provider = WPGMZA.Geocoder.Providers.GOOGLE_MAPS; 64 break; 65 case 'open-layers': 66 case 'open-layers-latest': 67 provider = WPGMZA.Geocoder.Providers.OPEN_LAYERS; 68 break; 69 case 'leaflet': 70 case 'leaflet-stadia': 71 case 'leaflet-maptiler': 72 case 'leaflet-zerocost': 73 provider = WPGMZA.Geocoder.Providers.LEAFLET; 74 break; 75 break; 76 case 'leaflet-azure': 77 /* Azure */ 78 provider = WPGMZA.Geocoder.Providers.AZURE_MAPS; 79 break; 80 case 'leaflet-locationiq': 81 /* LocationIQ */ 82 provider = WPGMZA.Geocoder.Providers.LOCATION_IQ; 83 break; 84 } 85 } 86 } else if (typeof provider === 'string'){ 87 switch(provider){ 88 case 'google-maps': 89 provider = WPGMZA.Geocoder.Providers.GOOGLE_MAPS; 90 break; 91 case 'nominatim': 92 provider = WPGMZA.Geocoder.Providers.NOMINATIM; 93 if(WPGMZA.settings && WPGMZA.settings.engine){ 94 /* Sub filter by engine, just to ensure we get the correct extension */ 95 switch(WPGMZA.settings.engine){ 96 case 'open-layers': 97 case 'open-layers-latest': 98 provider = WPGMZA.Geocoder.Providers.OPEN_LAYERS; 99 break; 100 case 'leaflet': 101 case 'leaflet-stadia': 102 case 'leaflet-maptiler': 103 case 'leaflet-zerocost': 104 provider = WPGMZA.Geocoder.Providers.LEAFLET; 105 break; 106 } 107 } 108 break; 109 case 'azure-maps': 110 provider = WPGMZA.Geocoder.Providers.AZURE_MAPS; 111 break; 112 case 'location-iq': 113 provider = WPGMZA.Geocoder.Providers.LOCATION_IQ; 114 break; 115 } 116 } 117 118 switch(provider){ 119 case WPGMZA.Geocoder.Providers.NOMINATIM: 120 return WPGMZA.NominatimGeocoder; 121 break; 122 case WPGMZA.Geocoder.Providers.OPEN_LAYERS: 123 return WPGMZA.OLGeocoder; 124 break; 125 case WPGMZA.Geocoder.Providers.LEAFLET: 126 return WPGMZA.LeafletGeocoder; 127 break; 128 case WPGMZA.Geocoder.Providers.AZURE_MAPS: 129 return WPGMZA.AzureGeocoder; 130 break; 131 case WPGMZA.Geocoder.Providers.LOCATION_IQ: 132 return WPGMZA.LocationIQGeocoder; 133 break; 134 case WPGMZA.Geocoder.Providers.GOOGLE_MAPS: 135 default: 136 return WPGMZA.GoogleGeocoder; 137 break; 138 } 139 } 140 141 /** 142 * Creates an instance of a Geocoder, <strong>please <em>always</em> use this function rather than calling the constructor directly</strong> 143 * @method 144 * @memberof WPGMZA.Geocoder
145 * @return {WPGMZA.Geocoder} A subclass of WPGMZA.Geocoder 146 */ 147 WPGMZA.Geocoder.createInstance = function() 148 { 149 var constructor = WPGMZA.Geocoder.getConstructor(); 150 return new constructor(); 151 } 152 153 /** 154 * Attempts to convert a street address to an array of potential coordinates that match the address, which are passed to a callback. If the address is interpreted as a latitude and longitude coordinate pair, the callback is immediately fired. 155 * @method 156 * @memberof WPGMZA.Geocoder 157 * @param {object} options The options to geocode, address is mandatory. 158 * @param {function} callback The callback to receive the geocode result. 159 * @return {void} 160 */ 161 WPGMZA.Geocoder.prototype.getLatLngFromAddress = function(options, callback) 162 { 163 if(WPGMZA.isLatLngString(options.address)) 164 { 165 var parts = options.address.split(/,\s*/); 166 var latLng = new WPGMZA.LatLng({ 167 lat: parseFloat(parts[0]), 168 lng: parseFloat(parts[1]) 169 }); 170 171 // NB: Quick fix, solves issue with right click marker. Solve this there by making behaviour consistent 172 latLng.latLng = latLng; 173 174 callback([latLng], WPGMZA.Geocoder.SUCCESS); 175 } 176 } 177 178 /** 179 * Attempts to convert latitude eand longitude coordinates into a street address. By default this will simply return the coordinates wrapped in an array. 180 * @method 181 * @memberof WPGMZA.Geocoder 182 * @param {object} options The options to geocode, latLng is mandatory. 183 * @param {function} callback The callback to receive the geocode result. 184 * @return {void} 185 */ 186 WPGMZA.Geocoder.prototype.getAddressFromLatLng = function(options, callback) 187 { 188 var latLng = new WPGMZA.LatLng(options.latLng); 189 callback([latLng.toString()], WPGMZA.Geocoder.SUCCESS); 190 } 191 192 /** 193 * Geocodes either an address or a latitude and longitude coordinate pair, depending on the input 194 * @method 195 * @memberof WPGMZA.Geocoder 196 * @param {object} options The options to geocode, you must supply <em>either</em> latLng <em>or</em> address. 197 * @throws You must supply either a latLng or address 198 * @return {void} 199 */ 200 WPGMZA.Geocoder.prototype.geocode = function(options, callback) 201 { 202 if("address" in options) 203 return this.getLatLngFromAddress(options, callback); 204 else if("latLng" in options) 205 return this.getAddressFromLatLng(options, callback); 206 207 throw new Error("You must supply either a latLng or address"); 208 } 209 210});
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.