简体   繁体   English

如何查看圆形标记中是否带有标记?

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

I need to check every 5 seconds if a marker(user) is now contained in a circleMarker(loc) 我需要每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;
                }           
            }
        }

Above is the current code I have, which is called by this: 上面是我拥有的当前代码,被称为:

var start = setInterval(updateLocation, 5000);

Thanks in advance, Ed. 在此先感谢,Ed。

It seems that you're trying to do geofencing , but in a weird way. 看来您正在尝试进行地理围栏 ,但方法很怪异。 Why rely on the drawn appearance of CircleMarker s when all you want is to know if the distance from the user to something is less than a threshold (the CircleMarker radius)? 当您只想知道用户到某物的距离是否小于阈值( CircleMarker半径)时,为什么要依赖CircleMarker的绘制外观?

Just use the distance method of L.Map : 只需使用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);

If your loc variable is really an L.circleMarker , please realize that by definition a marker is a point / place, ie it does not have any area , hence no bounds . 如果您的loc变量确实是L.circleMarker ,请意识到根据定义, 标记是一个点/位置,即它没有任何区域 ,因此没有界限 You probably meant to use an L.circle for loc in the first place. 您可能首先打算将L.circle用于loc

In Leaflet 0.x (eg version 0.7.7), the L.circleMarker inherits from L.circle , therefore it does have a .getBounds() method, even though it is meaningless. 在Leaflet 0.x(例如0.7.7版)中, L.circleMarker继承自L.circle ,因此它确实具有.getBounds()方法,即使它没有意义。 Actually it returns a null area (southWest coordinates equal northEast coordinates equal the marker position). 实际上,它返回一个空区域(西南坐标等于东北坐标等于标记位置)。

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

So your code would actually evaluate to true iif the user location is exactly the same as your loc . 所以,你的代码实际上会评估为true IIF用户位置是完全一样的loc

In Leaflet 1.x (eg current version 1.0.1), this inconsistency has been corrected, and L.circleMarker no longer has the .getBounds() method. 在Leaflet 1.x(例如当前版本1.0.1)中,此矛盾已得到纠正,并且L.circleMarker不再具有.getBounds()方法。

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

(zoom in and out to see the radius of the red circle marker adjusting to keep the same size in pixels, until at high zoom, the marker is out of it. Whereas the blue circle has a radius which scales according to the zoom, since it represents an actual length; therefore it always contains (or not) the marker) (放大和缩小以查看红色圆圈标记的半径,进行调整以保持相同的像素大小,直到高倍缩放为止,标记不在其中。蓝色圆圈的半径根据缩放而缩放,因为它代表实际长度;因此,它始终包含(或不包含)标记)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM