I was having some problem with Promise in JavaScript. What I am trying to do is I got a list of address, then for each address, I need to call the geocode API to get the lat lng, then I will proceed to plot the markers together with the heatmap. Here is my code:
let promiseKey = Promise.all(
result.map()
);
var addedMarkers = promiseKey.then(
markers => Promise.all(
markers.map()
)
)
.then(drawForecastHeatmap);
The part where I call the geocode API:
function getBranchLatLng(address, branchName, total, queKey){
return new Promise(function(resolve){
var key = jQuery.rand(geoCodKeys);
var url = 'https://maps.googleapis.com/maps/api/geocode/json?key='+key+'&address='+address+'&sensor=false';
$.ajaxq (qyName, {
url: url,
dataType: 'json'
}).done(function( data ) {
var address = getParameterByName('address', this.url);
var index = errorArray.indexOf(address);
try{
var p = data.results[0].geometry.location;
var latlng = new google.maps.LatLng(p.lat, p.lng);
var markerItem =
{
'lat': p.lat,
'lng': p.lng,
'address': address,
'branchName': branchName,
'total': total,
};
console.log(markerItem);
resolve(markerItem);
if (index > -1) {
errorArray.splice(index, 1);
}
}catch(e){
if(data.status = 'ZERO_RESULTS')
return false;
//on error call add marker function for same address and keep in Error ajax queue
getBranchLatLng( address, 'Error' );
if (index == -1) {
errorArray.push( address );
}
}
});
//mentain ajax queue set
queuCounter++;
if( queuCounter == setLimit ){
queuCounter = 0;
}
});
}
The problem now is, the program just stopped at getBranchLatLng() and never even go into the addForecastMarker() although I managed to print out some lat lng from geocode.
Some of the address returning me:
jquery.min.js:4 GET https://maps.googleapis.com/maps/api/geocode/json?key=&address= 400 ()
Then when I try to extend the link, I am getting this:
error_message: "Invalid request. Missing the 'address', 'bounds', 'components', 'latlng' or 'place_id' parameter."
results :[]
status: "INVALID_REQUEST"
Any ideas on how to resolve those address with 'INVALID_REQUEST' back to the Promise parent?
Thanks!