I have a problem when executing a function that runs a for loop with 3 global variables that will be passed to a callback function. All the arrays have the same length, the problem comes with the 3 window variables, called in getPickup() inside the callback function don't change value in any of the loop goes. I find it strange because those variables should get a new value updated from location when the callback function is finished, and the loop is run again.
function calculateDistanceDriverCustomer() {
{% autoescape off %}
var locations = {{ locations }}
{% endautoescape %}
{% autoescape off %}
var firstname = {{ firstname }}
{% endautoescape %}
{% autoescape off %}
var lastname = {{ lastname }}
{% endautoescape %}
{% autoescape off %}
var rating = {{ rating }}
{% endautoescape %}
var location;
for (location = 0; location < locations.length; location++) {
var origin = locations[location];
window.firstname = firstname[location];
window.lastname = lastname[location];
window.rating = rating[location];
var destination = $('#origin-input').val();
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [origin],
destinations: [destination],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC, // kilometers and meters.
avoidHighways: false,
avoidTolls: false
}, callback);
}
};
function callback(response, status) {
if (status != google.maps.DistanceMatrixStatus.OK) {
$('#result').html(err);
} else {
var origin = response.originAddresses[0];
var destination = response.destinationAddresses[0];
if (response.rows[0].elements[0].status === "ZERO_RESULTS") {
$('#result').html("Better get on a plane. There are no roads between " + origin + " and " + destination);
} else {
//get estimated pickup time if distance less or equal than 10km
function getPickup() {
var distance = response.rows[0].elements[0].distance;
var duration = response.rows[0].elements[0].duration;
var distance_in_kilo = distance.value / 1000;
var duration_value = duration.value*1000;
console.log(distance_in_kilo);
var time = new Date()
time = new Date(time.getTime() + duration_value);
var date = new Date(time)
var currenthr = date.getHours()
var currentmin = date.getMinutes()
var currentsec = date.getSeconds()
if (currenthr < 10) {currenthr = "0"+currenthr;}
if (currentmin < 10) {currentmin = "0"+currentmin;}
if (currentsec < 10) {currentsec = "0"+currentsec;}
window.pickupTime = currenthr + ":" + currentmin + ":" + currentsec;
console.log(pickupTime);
if (distance_in_kilo <= 3000) {
var name = document.createElement("hd");
var brk = document.createElement("br");
var node = document.createTextNode(window.firstname + " " +
window.lastname);
name.appendChild(node);
var element = document.getElementById("cars");
element.appendChild(name);
element.appendChild(brk);
var pickup = document.createElement("hd");
var node1 = document.createTextNode("Pickup Time: " + window.pickupTime);
pickup.appendChild(node1);
var element1 = document.getElementById("cars");
element1.appendChild(pickup);
element1.appendChild(brk);
var rating = document.createElement("hd");
var node3 = document.createTextNode("rating: " + window.rating);
rating.appendChild(node3);
var element3 = document.getElementById("cars");
element3.appendChild(rating);
element3.appendChild(brk);
}
else {
console.log("Not available");
}
}
getPickup();
}
}
};
The variables are defined at the beginning of the for loop, they are window.firstname,window.lastname and window.rating. They get the value from an array whose contents have been passed from python(defined at beginning of calculateDistanceDriverCustomer().
This is the python code:
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute("SELECT CurrentLocation, FirstName, LastName, OverallRating FROM driver WHERE OnJourney=0")
rows = cursor.fetchall() # data from database
locations = []
firstname = []
lastname = []
rating = []
for row in rows:
locations.append(row['CurrentLocation'])
firstname.append(row['FirstName'])
lastname.append(row['LastName'])
rating.append(row['OverallRating'])
return render_template("Search.html", rows=rows, locations=locations, firstname=firstname, lastname=lastname, rating=rating)
Thanks for your help