简体   繁体   中英

how to make proximity alert fire only once when enter radius?

I have created an app in which the user first has an option to add latitude and longitude to SQLite database to fire Notification when they enter registered area.

After that i have added marker using that LatLng saved in database. I have used Notification to show the alert.

Problem : My problem is it's keep firing notification if i stay for some time within the marker radius.

Anyone who knows a solution to make it fire only once if i enter the proximity and fire again when i re-enter the area only. Thanks in advance.

The simple solution is just to save whether or not you have fired an notification, and then reset it when you first notice that you are outside the area:

bool notified = false;
if(inArea() && !notified)
    notify();
    notify = true;
else if(!inArea and notified)
   notified= false;

Calculate the difference between Circle, center point and where it is present:

Location locationA = new Location("point A");     
locationA.setLatitude(latitude_center); 
locationA.setLongitude(longitude_center);

Location locationB = new Location("point B");
locationB.setLatitude(lat_current); 
locationB.setLongitude(lng_current);

float distance = locationA.distanceTo(locationB);

if(radius < distance){
// fire alert
}

You probably ought to look at including a degree of hysterisis ... an overlap between when you enter the area and when you leave. You need two radii: the smaller defines when you have entered the target area; the larger defines when you leave it. You then incorporate these into something similar to Astrogat 's answer:

bool notified = false;
...
if(inArea(innerRadius) && !notified) {
    notify();
    notify = true;
} else if(!inArea(outerRadius) && notified) {
   notified= false;
}

The advantage over a single radius is that this will prevent you toggling between "in" and "out" when you're just on the edge of the target area.

Note, also, that the notified variable will need to persist across calls to the "should we notify" logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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