如何检查当前位置是否在设定的半径之外?

问题描述 投票:0回答:2

我目前正在开发一个应用程序,当某人在一个正方形内时会触发通知。下面定义了正方形:

var zonebounds = [[30,35], [40,45]];
var zone = L.rectangle(zonebounds, {color: "#ff7800", weight: 1, oppacity: .5});

我已发表以下声明,以检查是否有人在广场内。

if (posY > zonebounds[0][0] && posY < zonebounds[1][0] && posX > zonebounds[0][1] && posX < zonebounds[1][1] && zoneTimer == 0) {
    ons.notification.toast('Test', { timeout: 5000 });
    zoneTimer = 1;
} else if (posY >! zonebounds[0][0] && posY <! zonebounds[1][0] && posX >! zonebounds[0][1] && posX <! zonebounds[1][1] && zoneTimer == 1) {
    zoneTimer = 0;
}

[我认为>!可悲的是没有我想要的表现:')。我已经设置了zoneTimer变量,以便通知不会重复。也许有一种更好的方法可以做到这一点:)预先感谢!

javascript leaflet onsen-ui
2个回答
0
投票

您可以取><=的反面进行检查。

对于第二次检查,您需要OR条件。

if (
    posY > zonebounds[0][0] && posY < zonebounds[1][0] &&
    posX > zonebounds[0][1] && posX < zonebounds[1][1] &&
    zoneTimer == 0
) {
    ons.notification.toast('Test', { timeout: 5000 });
    zoneTimer = 1;
} else if (
    (posY <= zonebounds[0][0] || posY >= zonebounds[1][0] || 
    posX <= zonebounds[0][1] || posX >= zonebounds[1][1]) &&
    zoneTimer == 1
) {
    zoneTimer = 0;
}

0
投票

您可以用传单检查点是否在矩形中。

var bounds = L.latLngBounds(zonebounds);

if(bounds.contains(latlng)){
    console.log("notify");
}else{
    console.log("nothing");
}

如果您的点(posX和posY)是像素,则可以使用它进行转换:

var point = L.point(posX ,posY); // x=0,y=0
var latlng = map.layerPointToLatLng(point);
© www.soinside.com 2019 - 2024. All rights reserved.