javascript - 如何查看标记是否位于圆圈标记内?

标签 javascript html cordova leaflet

我需要每 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);

提前致谢,埃德。

最佳答案

看来你正在尝试做 geofencing ,但是以一种奇怪的方式。当您只想知道用户到某物的距离是否小于阈值(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);

关于javascript - 如何查看标记是否位于圆圈标记内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39946087/

相关文章:

javascript - 选择 Mongoose 模式中的所有字段

javascript - 如果选中另一个复选框,则选中复选框

javascript - 使用 rowIndex 和 cellIndex 更改表格单元格中的文本

windows-phone-7 - Windows Phone 7.5/8 推送通知和 PhoneGap

ios - 如何处理ios应用程序的 ionic 无限重载

cordova - 人行横道和 Cordova 整合

javascript - $compile 显示未捕获的 TypeError : undefined is not a function

html - CSS 最小宽度和最大宽度问题

php - 如何使用 mysql 查询发布新闻

javascript - 如何使用 Javascript 检查不同下拉列表中的选定值是否相同