0

I have the bellow code checking if a map marker is inside or outside of a geofence.

i am going to make it alert detected out of the bounds.

My problem is the map markers a refreshing constantly and i do not want the alert to be repeated over an over.

I need to set something when the alarm is played. Then only do the alert if that thing is unset.

When the user is detected back inside the bounds it will unset it also.

    if (name === f.contact) {
        var fence = new google.maps.LatLng(f.lat, f.lng);
        var dist = google.maps.geometry.spherical.computeDistanceBetween(posi, fence);
        // check if in/out of fence
        if (dist > f.radius) {
            console.log(f.contact+" : "+dist+" meters - outside the fence");
            // OMG outside the fence play an alarm
        } else {
            console.log(f.contact+" : "+dist+" meters - inside the fence");
            // Back inside the fence, reset the alarm
        }
    }

i was thinking possibly making an array like this

var alertSent = [];

and then if outside the geofence adding the users name to it

alertSent.push(name);

how would i check if the name exists in the array?

and how would i delete the name from the array when back inside the fence?

4

2 に答える 2

1

配列を使用することになった場合は、次のような文字列が見つかるまで、すべてのインデックスを検索する必要があります

配列に JavaScript のオブジェクトが含まれているかどうかを確認するにはどうすればよいですか? または 項目が JavaScript 配列にあるかどうかを確認する最良の方法は?

この問題を処理するためにイベントとイベントリスナーを登録することも考えられます。それはより良い設計になるでしょう。

または、使用するようなハッシュマップの種類の JavaScript 実装を使用することもできます

alertSent["driver1"]=true;,

この場合、ルックアップは単純で、 を使用するだけです。

alertSent["driver1"]

ブール値を取得します。ただし、この場合は配列スペースに注意してください。

于 2012-10-12T15:05:02.070 に答える
1

オブジェクトを連想配列として使用し、名前をキーとして使用し、送信済み/未送信のブール値を使用できます。また、alertSent[name] に name がまったく含まれていない場合、false 値に評価されます。

var alertSent = {};

// if user outside: check
if (!alertSent[name]) {
    // show alert
    // remember that alert was shown
    alertSent[name] = true;
}

// remove name from alertSent:

alertSent[name] = false;
于 2012-10-12T15:05:42.273 に答える