Alex Alex - 6 months ago 130
Javascript Question

Update marker position on google map without page load

I'm trying to update the google map marker position in every 20 seconds which comes from database but it's doesn't work.

Code :

for (i = 0; i < purple.length; i++) {

if (purple[i][1] == latitude && purple[i][2] == longitude) {
nogreen = 1;
}

marker = new google.maps.Marker({
position : new google.maps.LatLng(purple[i][1], purple[i][2]),
map : map,
title : purple[i][0],
data : purple[i][0],
zoom : maxZoomService,
icon : 'img/purple.png',
shadow : 'img/purple.png'
});

setInterval(function () {
position : new google.maps.LatLng(purple[i][1], purple[i][2]),
marker.setPosition(position);
}, 20000);
};


Is this correct or how can i do that ?

Answer

This code here isn't valid javascript:

setInterval(function() {
        position: new google.maps.LatLng(purple[i][1], purple[i][2]),
        marker.setPosition(position);
    }, 20000); 

You want to do:

setInterval(function() {
        position = new google.maps.LatLng(purple[i][1], purple[i][2]);
        marker.setPosition(position);
    }, 20000);