簡體   English   中英

如何刪除傳單上的當前標記並再次添加新標記?

[英]How to remove current marker on leaflet and add a new marker again?

我的代碼有問題。 我在地圖上顯示標記,但是無需更改標記位置,它只會在地圖上添加新標記。

我有問題的打印屏幕。

在此處輸入圖片說明

如何刪除上一個標記?

這是我的代碼:

var latB = 0;
var lonB = 0;
var mark = 0;
var marker = null;
function showOnMap(a){
    convert_location(a);
    marker = L.marker([latB, lonB]).bindPopup(a);
    map.removeLayer(marker)
    map.addLayer(marker);
}

function convert_location(a){
    var toData = (function (){
            var toData = null;
                $.ajax({
                    'async': false,
                    'global': false,
                    'url': 'http://nominatim.openstreetmap.org/search?format=json&limit=5&q='+a,
                    'dataType': 'json',
                    'success': function(data){
                        toData = data;
                    }
                });
            return toData;
    })();

    $.each(toData, function(key, val){
        latB = val.lat;
        lonB = val.lon;
    });

}

在設置對新標記的引用后,您將擦除標記,因此基本上,您是在刪除不存在的新標記,然后將其添加到地圖中。 重新分配標記之前,應將其從地圖上刪除。

function showOnMap(a){
  convert_location(a);
  if (marker != null) map.removeLayer(marker);
  marker = L.marker([latB, lonB]).bindPopup(a);
  map.addLayer(marker);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM