【问题标题】:HTML5, geolocation in real time with markerHTML5,带标记的实时地理定位
【发布时间】:2015-06-18 22:26:23
【问题描述】:

我有这个代码:

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


    };

}

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

【问题讨论】:

  • 您可以删除此标记并使用 setInterval() 调用您的 startTracking
  • 如果我删除标记,我在哪里写标记的创建?调用函数后?

标签: javascript html google-maps-api-3 marker


【解决方案1】:

插入一个刷新按钮并解决问题..

【讨论】:

    【解决方案2】:

    我真的不知道 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);
    

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

    【讨论】:

    • 我用这个'marker.setMap(null);'用于销毁标记,但现在我什么也没看到
    • 我不知道,我正在使用类似的东西,但使用的是 openlayers。这个想法是删除它并重新创建它。您必须对此进行搜索。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 2011-06-21
    • 1970-01-01
    • 2015-11-28
    • 2015-03-27
    相关资源
    最近更新 更多