I have a javascript code which calculates the distance between the user's location and an array of fuzzy addresses. It uses the distance matrix from the google maps api if I am not wrong. On the success of the getDistanceMatrix function a callback function is asynchronously called. Now, I need to be able to pass a particular value to this call back function so that I am able to replace the corresponding table row.
function getDistance(position){
origin = new google.maps.LatLng(parseFloat(position.coords.latitude),parseFloat(position.coords.longitude));
for (var i=0;i<mpoints_arr.length;i++){
destination = mpoints_arr[i][2];
service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [origin],
destinations: [destination],
travelMode: google.maps.TravelMode.DRIVING,
avoidHighways: false,
avoidTolls: false
},
callback(i)
);
}
}
function callback(response, status, j){
if(status == "OK"){
alert(response.rows[0].elements[0].distance.text);
console.log(j);
//rows[i].getElementsByTagName('td')[3].innerHTML = response.rows[0].elements[0].distance.text;
} else {
alert("Error: " + status);
}
}
However, the object 'j' is said to be undefined in the log. Awaiting a solution. Thanks.