问题
How to implement Geofence for tracking another users?
I am developing an application which will track family members, and I want to implement Geofence feature which will enable user to do for ex:
user A will track user B
user A will determine the geofence area and when user B will enter this geofence user A will get a notification
I retreived the location of user B every 10 minutes and I want to create Geofence for user B which will notify user A when user B enter the geofence area
I try to use google Geofence like this code :
mGeofenceList.add(new Geofence.Builder()
// Set the request ID of the geofence. This is a string to identify this
// geofence.
.setRequestId(entry.getKey())
.setCircularRegion(
entry.getValue().latitude,
entry.getValue().longitude,
Constants.GEOFENCE_RADIUS_IN_METERS
)
.setExpirationDuration(Constants.GEOFENCE_EXPIRATION_IN_MILLISECONDS)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
Geofence.GEOFENCE_TRANSITION_EXIT)
.build());
but it just work for the user device "user A location" not for another user location.
回答1:
I guess you should add a database and keep track of who created geofence on whom. Also store geofence radius and center point in the database. In order to check if user is inside geofence, calculate distance from user location coordinates and all geofences coordinates and then compare the distance with radius in 5 minutes span. If distance < radius, user is inside else outside. If user is inside set the flag to avoid repeated notifications. If user goes outside reset the flag. I think this should work.
来源:https://stackoverflow.com/questions/40599438/android-how-to-make-user-create-geofence-for-another-tracked-users