HTML5, 带标记的实时地理定位

HTML5, geolocation in real time with marker

我有这个代码:

function startTracking() {

   var trackId = navigator.geolocation.watchPosition(successCallback, null, {maximumAge:1000, timeout:60000, enableHighAccuracy:true});

    function successCallback(position){
        map.panTo(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));



            var marker = new google.maps.Marker({
                position:  new google.maps.LatLng(position.coords.latitude, position.coords.longitude),
                map: map,
                title: 'Posizione Attuale'

            });
            marker.setMap(map);  


    };

}

现在,我每次移动时应用程序都会创建一个标记。我希望在地图上,我只看到我当时所在的标记,而不会看到之前位置的标记。

我真的不知道 api 但是例如 :

function startTracking() {

 //Destroy the marker here

var trackId = navigator.geolocation.watchPosition(successCallback, null, {maximumAge:1000, timeout:60000, enableHighAccuracy:true});

function successCallback(position){
    map.panTo(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));



        var marker = new google.maps.Marker({
            position:  new google.maps.LatLng(position.coords.latitude, position.coords.longitude),
            map: map,
            title: 'Posizione Attuale'

        });
        marker.setMap(map);  


};

}


setInterval(startTracking, 3000);

您可能希望将标记设为全局

插入刷新按钮,解决问题..