Alert message once when close to POI

You must Login before you can answer or comment on any questions.

My program checks every 30 seconds if my location =< 200 meter from POI. When this is true then I want a alert message, but I want that alert message only one time. If my location is > 200 meter then nothing, but when I'm again =< 200 meter again a alert message only 1 time. Can someone please help me white this problem. Some code examples will be helpful.

1 Answer

Hi Henri

Your explanation is not very clear, but I think you want to make sure that you ONLY alert someone once until you re-enter the area after leaving it.

If this is correct, simply add a variable that says CanInform, then when you are less than 200 meters check if you CanInform, if you can then alert them, change CanInfo to false. If another even fires and you are still inside 200 meters then when you try and inform again you cannot as the CanInform is set to false. Once you go outside 200 meters set CanInform back to true.

So VERY simple example;

// place this outside of all events
var boolCanInform = true;
 
// less than 200 meters event
if (boolCanInform === true) {
    boolCanInform === false;
    alert('Inside');
}
 
// more than 200 meters event
boolCanInform === true;
As simple as that.

— answered 10 months ago by Malcolm Hollingsworth
answer permalink
5 Comments
  • No, when I'm in the area of 200 meters I want the alert 1 time. When I'm out of the area he must reset so when I go back in the area of 200 meters I want the alert message again 1 time.

    — commented 10 months ago by Henri Grotenhuis

  • Hi Henri

    Other than my typo that is EXACTLY what happens in the example, corrected version below;

    // place this outside of all events
    var boolCanInform = true;
     
    // less than 200 meters event
    if (boolCanInform === true) {
        boolCanInform = false;
        alert('Inside');
    }
     
    // more than 200 meters event
    boolCanInform = true;
    This code shows that the app first sets a variable called boolCanInform and gets sets it to the correct value of true.

    Now when someone triggers the less than 200 meters event - the code asks the variable boolCanInform if the user can be informed - currently this is set to true so they are alerted. Then as they have been informed ONCE the variable boolCanInform is set to false.

    Now say the trigger for less than 200 meters is called AGAIN, the event code checks the value of boolCanInform which now says false so NO ALERT is made.

    Later on the person goes outside the 200 meters and that event is triggered. This simply set boolCanInform to be true ready for the next potential time the perosn goes back into the 200 meters.

    Now the person re-enters the 200 meters or less zone which calls that event, the variable boolCanInform is checked and it has says true so the person is alerted, and the variable is reset back to false.

    So the alert will ONLY fire once no matter how many times the less than 200 meter event is fired. But if they go OUTSIDE the 200 meters area and then back in - then the alert will fire again ONLY once.

    This is exactly as you asked.

    — commented 10 months ago by Malcolm Hollingsworth

  • Great, I used this code and it works, thank you.

    — commented 10 months ago by Henri Grotenhuis

  • Show 2 more comments

Your Answer

Think you can help? Login to answer this question!