簡體   English   中英

如何查看圓形標記中是否帶有標記?

[英]How can I see if a marker is with in a circle marker?

我需要每5秒鍾檢查一次circleMarker(loc)中是否包含一個標記(用戶)

function updateLocation(){
            if(!isSet){clearInterval(start)}else{
                user.setLatLng(new L.LatLng(lat, lng));
                if(loc.getBounds().contains(new L.LatLng(lat, lng))){
                    document.getElementById('setButton').style.background = 'purple'
                    soundAlarm();
                    isSet = false;
                }           
            }
        }

上面是我擁有的當前代碼,被稱為:

var start = setInterval(updateLocation, 5000);

在此先感謝,Ed。

看來您正在嘗試進行地理圍欄 ,但方法很怪異。 當您只想知道用戶到某物的距離是否小於閾值( CircleMarker半徑)時,為什么要依賴CircleMarker的繪制外觀?

只需使用L.Mapdistance方法:

var guardedLocation = L.latLng(...);
var thresholdDistance = 100;    // In meters
var start;

function updateLocation(){
    var userPosition = L.latLng(lat, lng);
    user.setLatLng(userPosition);

    if(map.distance(userPosition, guardedLocation) <= thresholdDistance) {
        document.getElementById('setButton').style.background = 'purple'
        soundAlarm();
        isSet = false;
        clearInterval(start);
    }
}

start = setInterval(updateLocation, 5000);

如果您的loc變量確實是L.circleMarker ,請意識到根據定義, 標記是一個點/位置,即它沒有任何區域 ,因此沒有界限 您可能首先打算將L.circle用於loc

在Leaflet 0.x(例如0.7.7版)中, L.circleMarker繼承自L.circle ,因此它確實具有.getBounds()方法,即使它沒有意義。 實際上,它返回一個空區域(西南坐標等於東北坐標等於標記位置)。

演示: https : //jsfiddle.net/y63u5utf/2/

所以,你的代碼實際上會評估為true IIF用戶位置是完全一樣的loc

在Leaflet 1.x(例如當前版本1.0.1)中,此矛盾已得到糾正,並且L.circleMarker不再具有.getBounds()方法。

演示: https : //jsfiddle.net/y63u5utf/1/

(放大和縮小以查看紅色圓圈標記的半徑,進行調整以保持相同的像素大小,直到高倍縮放為止,標記不在其中。藍色圓圈的半徑根據縮放而縮放,因為它代表實際長度;因此,它始終包含(或不包含)標記)

暫無
暫無

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

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