删除谷歌地图标记

我有下一个脚本

$('.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); }); 

并且它每隔1秒返回一次从数据库保存的信息与来自gps发送者的坐标,我将它们加载到谷歌地图中,​​但它创建了一个新标记而不是替换创建的最后一个标记。

我需要删除最后生成的标记或替换为1秒后生成的新标记。 有任何想法吗?

每次都不要创建新标记,如果它已经存在,则将其移动到新位置或在创建新标记之前隐藏它。

 // 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 )); }