window.findLL = function() {
var address = new Array();
var longlat = new Array();
var j = 0;
$('.place').each(function()
{
address[j] = $(this).val();
j++;
});
for(var i=0;i<address.length;i++)
{
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address[i]}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
var location = results[0].geometry.location;
alert('LAT: ' + location.lat() + ' LANG: ' + location.lng());
longlat[i] = location.lat()+'~'+location.lng();
}
});
}
distanceArrayFinal = findDistance(longlat);
}
I am trying to find out the distance between some addresses. I am using geocode() for latitudes and longitudes. findDistance() is the method for finding distances for which I am sending an array of inputs which I would get from geocode(). But findDistance() method is being called before I could get the longitude and latitudes but I need to call that method only after getting the latitudes and longitudes of all address. Can anyone please help me with this, either to find out the end of response from geocode() for all addresses and then call findDistance() or inserting some delay before calling findDistance() so that I would get all latitudes and longitudes.
var promise = new Promise(function(resolve, reject) {
directionsService.route(request, function(response, status)
{
if (status == google.maps.DirectionsStatus.OK)
{
resolve(request.response);
}
else
{
reject(Error(request.statusText));
}
});
});
promise.then(function(response) {
distance = response.routes[0].legs[0].distance.text;
}, function(error) {
console.log('Promise rejected.');
console.log(error.message);
});
I tried promises like above, I am getting the response as undefined in "promise.then(function(response)". Am I doing anything wrong?