Reputation: 187
I have the next script
$('.check_orders_dl').click(function(){
var get_maps = function(){
$.ajax({
type: "POST",
url:"/tracking.php",
success:function(data, textStatus, jqXHR) {
var objs = jQuery.parseJSON( data );
var obj = objs[0];
$('.lat').val(obj.latitude);
$('.long').val(obj.longitude);
},
error: function(jqXHR, textStatus, errorThrown){
}
});
};
var latitude = $('.lat').val();
var longitude = $('.long').val();
var markers = [];
var map,
mapCenter = new google.maps.LatLng(40.700683, -73.925972),
map;
function initializeMap()
{
map = new google.maps.Map(document.getElementById('map_canvas'), {
zoom: 16,
center: mapCenter,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
}
function setCurrentPosition() {
var latitude = $('.lat').val();
var longitude = $('.long').val();
var markers = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(
latitude,
longitude
),
title: "Current Position"
});
map.panTo(new google.maps.LatLng(
latitude,
longitude
));
}
function initLocationProcedure() {
initializeMap();
}
initLocationProcedure();
setInterval(setCurrentPosition,1000);
setInterval(get_maps, 1000);
});
And it's giving me back each 1second the information saved from database with the coordates from the gps sender, I loaded them into google maps, but it's creating a new mark instead of replacing the last mark created.
I need to delete the last generated mark or replace with the new one generated after 1 second. Any ideas?
Upvotes: 1
Views: 117
Reputation: 161334
Don't create a new marker every time, if it already exists either move it to the new location or hide it before creating the new one.
// in the global scope
marker;
function setCurrentPosition() {
var latitude = $('.lat').val();
var longitude = $('.long').val();
if (marker && marker.setPosition) {
// not the first time, move the existing marker
marker.setPosition(new google.maps.LatLng(
latitude,
longitude
));
} else { // first time, create a marker
marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(
latitude,
longitude
),
title: "Current Position"
});
map.panTo(new google.maps.LatLng(
latitude,
longitude
));
}
Upvotes: 1