Could someone tell me why my returned value is blank when the console.log returns the right answer?
I have a function that interates through a series of results and I'm trying to display the what3words name for the decimal degrees coordinates that I'm passing into this variable.
$.each(data.d.results, function (index, item) {
var whatThreeWords = fnWhatThreeWords(item.Latitude,item.Longitude);
//other code here to plot via leaflet
});
My fnWhatThreeWords looks like:
function fnWhatThreeWords(Lat,Lng) {
var whatThreeWords = "";
$.ajax({
type: "GET",
dataType: "json",
url: "https://api.what3words.com/v2/reverse?coords="+Lat+","+Lng+"&key=APIKEY&lang=en&format=json",
error: function (err) {
console.log(err);
},
success: function (data, status, xhr) {
console.log(data.words);
whatThreeWords = data.words;
}
});
return whatThreeWords;
}
The console.log returns the appropriate response: "crumples.harmlessly.measuring" but when I return the whatThreeWords global function variable the result is empty;
var point = new L.marker([item.Latitude, item.Longitude], {icon: color}).bindPopup("<b>Residence "+Edit+"</b><br>RPA Id: "+item.RPAId+"<br>"+item.Address+"<br>what3words: "+whatThreeWords, {autoClose:false});