I have problem with javascript code. I am using google maps and I am collecting data trough from server in json format. When I check data in console window in some browser I can see that data is actually coming.
My problem is that variable is read once and I am showing it’s value in the textbox. Now when I keep receiving data variable is always the same, any idea why? And I am 100% sure that I am getting different values.
Here is the code:
function updateTrucks() {
$.ajax({
url: "get_data.php",
cache: false,
dataType: 'json',
data: {},
error: function(jqxhr, textStatus, errorThrown) {
alert("Error: " + textStatus + " " + errorThrown);
},
success: function(data) {
for(var i = 0; i < data.results.length; i++) {
if(trucks.hasOwnProperty(data.results[i].id)) {
// update position
trucks[data.results[i].id].setPosition(new google.maps.LatLng(
parseFloat(data.results[i].lat),
parseFloat(data.results[i].lng)));
trucks[data.results[i].id].speed = data.results[i].speed;
}
else {
// code
}
google.maps.event.addListener(trucks[data.results[i].id], 'position_changed', function() {
if(typeof tracked !== "undefined") {
if(this.id == tracked.id) {
map.panTo(this.getPosition());
$("#speed").val(this.speed);
}
}
});
This line is not working correctly $("#speed").val(this.speed);
I think you need change order of you commands while updating position, so when position_changed fired speed property will be refreshed: