In my app I have a list of round 9 places with the associative lat long positions, and the end goal is to notify the user if they are within about half a mile of any of the locations.
What would be the best way to do this? This is what I have so far.
distanceFromListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
List<JSONObject> centres = sortVenuesByNearest(location);
Log.d("FUApp", "distanceFromListener setting location");
haveLocation = true;
theLocation = location;
float accuracy = location.getAccuracy();
for (JSONObject centre : centres) {
try {
Double distance = Double.valueOf(centre.getString("distanceTo"));
String name = centre.getString("name");
Integer venueID = Integer.valueOf(centre.getString("id"));
if (distance < 804.672) {
Bundle mBundle = new Bundle();
Double miles = (distance * 0.00062137);
DecimalFormat df = new DecimalFormat("#.##");
String miles2 = df.format(miles);
mBundle.putString(ExtraKeyCentreName, name);
mBundle.putString(ExtraKeyCentreDist, miles2 + " miles away");
mBundle.putString(ExtraKeyCentreJSON, centre.toString());
mBundle.putString(ExtraKeyCentrePostcode, "none");
sendNotification(ActivityCentrePage.class, "Visit " + name, "You're half a mile from " + name + ". Why not come for a visit?", R.drawable.ic_launcher, venueID, mBundle);
} else {
cancelNotification(venueID);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
Boolean notify_if_close = sp.getBoolean(SettingsNotifyIfClose, true);
if (notify_if_close) {
locationService.requestLocationUpdates(LocationManager.GPS_PROVIDER, 50, 100, distanceFromListener);
}
That works, but if the user cancels a notification saying there close, when the location is next updated it will pop up again, which will get annoying. Would a suitable alternative be put a big minTime, say half an hour on the requestLocationUpdates?
What would be the best way to do this?
Use geofences with the LocationClient
from the Play Services SDK, or try proximity alerts with LocationManager
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With