1/** 2 * Created by K.Gusz on 2017-04-18. 3 */ 4 5/* 6 TODO: add logging after merging to development 7 further refactoring of whatson.js 8 */ 9 10var locationService = (function () { 11 12 var googleApiKey = geolocationGoogleApiKey; 13 var bingApiKey = geolocationBingApiKey; 14 var queryTimeout = parseInt(geolocationTimeout); 15 16 var distanceUnitsMapping = { 17 10100: { 18 'unit': 'km', 19 'unitFormat': 'metric', 20 'word': 'km' 21 }, 22 10101: { 23 'unit': 'km', 24 'unitFormat': 'metric', 25 'word': 'km' 26 }, 27 10102: { 28 'unit': 'km', 29 'unitFormat': 'metric', 30 'word': 'km' 31 }, 32 10103: { 33 'unit': 'km', 34 'unitFormat': 'metric', 35 'word': 'km' 36 }, 37 10104: { 38 'unit': 'km', 39 'unitFormat': 'metric', 40 'word': 'km' 41 }, 42 10105: { 43 'unit': 'km', 44 'unitFormat': 'metric', 45 'word': 'km' 46 }, 47 10106: { 48 'unit': 'km', 49 'unitFormat': 'metric', 50 'word': 'km' 51 }, 52 10107: { 53 'unit': 'km', 54 'unitFormat': 'metric', 55 'word': 'km' 56 }, 57 10108: { 58 'unit': 'mil', 59 'unitFormat': 'imperial', 60 'word': 'miles' 61 }, 62 10109: { 63 'unit': 'mil', 64 'unitFormat': 'imperial', 65 'word': 'miles' 66 } 67 }; 68 69 var distanceUnits = function () { 70 return distanceUnitsMapping[tenantId]; 71 }; 72 73 var SortByDist = function (a, b) { 74 var aName = a.dist; 75 var bName = b.dist; 76 return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0)); 77 }; 78 79 var geoCodeAddressOSM = function (queryString) { 80 if (typeof queryString !== 'undefined' && queryString !== '') { 81 var queryUrl = '//nominatim.openstreetmap.org/search?q=' + queryString + '&format=json&polygon=0&addressdetails=0&limit=1&accept-language=en_gb'; 82 return $.when( 83 $.ajax({ 84 type: "get", 85 url: queryUrl, 86 contentType: "application/json; charset=utf-8", 87 timeout: queryTimeout, 88 dataType: "json" 89 }) 90 ); 91 } 92 }; 93 94 var geoCodeAddressOSMSuccess = function (data) { 95 var response = {}; 96 if (typeof data !== 'undefined' && data.length !== 0 && typeof data[0].lat !== 'undefined' && typeof data[0].lon !== 'undefined') { 97 response.status = 'OK'; 98 response.lat = data[0].lat; 99 response.long = data[0].lon; 100 } else { 101 response.status = 'ERROR'; 102 } 103 //console.log('geoCodeAddressOSM success callback fired:', response.status); 104 return response; 105 }; 106 107 var geoCodeAddressOSMError = function () { 108 var response = {}; 109 response.status = 'ERROR'; 110 //console.log('geoCodeAddressOSM error callback fired:', response.status); 111 return response; 112 }; 113 114 var geoCodeAddressGoogle = function (queryString) { 115 if (typeof queryString !== 'undefined' && queryString !== '') { 116 var country = _msg.show('countrycode').toLowerCase(); 117 var countryParam = ''; 118 if(country !== '') { 119 countryParam = '&address=' + country; 120 } 121 var queryUrl = '//maps.googleapis.com/maps/api/geocode/json?address=' + queryString + countryParam + '&key=' + googleApiKey; 122 return $.when( 123 $.ajax({ 124 type: "get", 125 url: queryUrl, 126 timeout: queryTimeout, 127 dataType: "json" 128 }) 129 ); 130 } 131 }; 132 133 var geoCodeAddressGoogleSuccess = function (data) { 134 var response = {}; 135 if (typeof data !== 'undefined' && typeof data.status !== 'undefined' && data.status === 'OK') { 136 if (typeof data.results !== 'undefined' && data.results.length !== 0) { 137 var result = data.results[0]; 138 if (typeof result.geometry !== 'undefined' && typeof result.geometry.location !== 'undefined') { 139 var location = result.geometry.location; 140 if (typeof location.lat !== 'undefined' && typeof location.lng !== 'undefined') { 141 response.status = 'OK'; 142 response.lat = data.results[0].geometry.location.lat; 143 response.long = data.results[0].geometry.location.lng; 144 } else { 145 response.status = 'ERROR'; 146 } 147 } else { 148 response.status = 'ERROR'; 149 } 150 } else { 151 response.status = 'ERROR'; 152 } 153 } else { 154 response.status = data.status; 155 } 156 //console.log('geoCodeAddressGoogle success callback fired:', response.status); 157 return response; 158 }; 159 160 var geoCodeAddressGoogleError = function (data) { 161 var response = {}; 162 response.status = 'ERROR'; 163 //console.log('geoCodeAddressGoogle error callback fired:', response.status); 164 return response; 165 }; 166 167 var geoCodeAddressBing = function (queryString) { 168 var country = _msg.show('countrycode').toUpperCase(); 169 var countryParam = ''; 170 if(country !== '') { 171 countryParam = '/' + country + '/'; 172 } 173 if (typeof queryString !== 'undefined' && queryString !== '') { 174 var queryUrl = 'https://dev.virtualearth.net/REST/v1/Locations' + countryParam + queryString + '?key=' + bingApiKey + '&s=1&o=json&maxResults=1'; 175 return $.when( 176 $.ajax({ 177 dataType: "jsonp", 178 jsonp: "jsonp", 179 timeout: queryTimeout, 180 url: queryUrl 181 }) 182 ); 183 } 184 }; 185 186 var geoCodeAddressBingSuccess = function (data) { 187 var response = {}; 188 if (typeof data !== 'undefined' && typeof data.statusCode !== 'undefined' && data.statusCode === 200) { 189 if (typeof data.resourceSets !== 'undefined' && data.resourceSets.length !== 0) { 190 if (typeof data.resourceSets[0].resources !== 'undefined' && data.resourceSets[0].resources.length !== 0) { 191 var resource = data.resourceSets[0].resources[0]; 192 if (typeof resource.point !== 'undefined') { 193 if (typeof resource.point.coordinates !== 'undefined' && resource.point.coordinates.length !== 0) { 194 response.status = 'OK'; 195 response.lat = resource.point.coordinates[0]; 196 response.long = resource.point.coordinates[1]; 197 } else { 198 response.status = 'ERROR'; 199 } 200 } else { 201 response.status = 'ERROR'; 202 } 203 } else { 204 response.status = 'ERROR'; 205 } 206 } else { 207 response.status = 'ERROR'; 208 } 209 } else { 210 response.status = 'ERROR'; 211 } 212 //console.log('geoCodeAddressBing success callback fired:', response.status); 213 return response; 214 }; 215 216 var geoCodeAddressBingError = function (data) { 217 var response = {}; 218 response.status = 'ERROR'; 219 //console.log('geoCodeAddressBing error callback fired:', response.status); 220 return response; 221 }; 222 223 var calculateDistance = function (lat1, lon1, lat2, lon2) { 224 var p = 0.017453292519943295; // Math.PI / 180 225 var c = Math.cos; 226 var a = 0.5 - c((lat2 - lat1) * p) / 2 + 227 c(lat1 * p) * c(lat2 * p) * 228 (1 - c((lon2 - lon1) * p)) / 2; 229 230 return 12742 * Math.asin(Math.sqrt(a)); // 2 * R; R = 6371 km 231 }; 232 233 var preciseRound = function (value, decPlaces) { 234 var val = value * Math.pow(10, decPlaces); 235 var fraction = (Math.round((val - parseInt(val)) * 10) / 10); 236 if (fraction === -0.5) fraction = -0.6; 237 val = Math.round(parseInt(val) + fraction) / Math.pow(10, decPlaces); 238 return val; 239 }; 240 241 var populateDistances = function (lat, long, locations) { 242 var results = []; 243 if (Object.prototype.toString.call(locations) === '[object Array]' && locations.length !== 0 && typeof parseFloat(lat) === 'number' && typeof parseFloat(long) === 'number') { 244 245 $.each(locations, function (idx, res) { 246 var distance = calculateDistance(lat, long, locations[idx].latitude, locations[idx].longitude); 247 if (distanceUnitsMapping[tenantId].unit === 'mil') { 248 distance = k2m(distance); 249 } 250 locations[idx].dist = preciseRound(distance, 2); 251 results.push(locations[idx]); 252 }); 253 results.sort(SortByDist); 254 results = results.slice(0, 15); 255 } else { 256 //console.log('error, no locations provided'); 257 } 258 return results; 259 }; 260 261 var k2m = function (kilometers) { 262 var km = parseFloat(kilometers); 263 var mi = 0; 264 if (!isNaN(km)) mi = km * 0.621371192; 265 return (mi); 266 }; 267 268 return { 269 calculateDistance: calculateDistance, 270 preciseRound: preciseRound, 271 k2m: k2m, 272 populateDistances: populateDistances, 273 geoCodeAddressOSM: geoCodeAddressOSM, 274 geoCodeAddressOSMSuccess: geoCodeAddressOSMSuccess, 275 geoCodeAddressOSMError: geoCodeAddressOSMError, 276 geoCodeAddressGoogle: geoCodeAddressGoogle, 277 geoCodeAddressGoogleSuccess: geoCodeAddressGoogleSuccess, 278 geoCodeAddressGoogleError: geoCodeAddressGoogleError, 279 geoCodeAddressBing: geoCodeAddressBing, 280 geoCodeAddressBingSuccess: geoCodeAddressBingSuccess, 281 geoCodeAddressBingError: geoCodeAddressBingError, 282 distanceUnits: distanceUnits() 283 } 284})(); 285
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.