Transiciones de transición de geofence de Android

Estoy construyendo una aplicación que incluye geofences googles, que he creado con las transiciones ENTER y EXIT . El problema se produce al activar " Location ", cuando dentro de mi geofence, que desencadena las dos transiciones, cuando sólo la transición ENTRAR debe disparar. He fijado ENTER y EXIT como intitialTrigger() .

¿Cómo esto puede ser posible?

¿Es una falla en el google api o he hecho algo mal en el constructor. Gracias por adelantado.

 @Override public void onResult(Result result) { Status s = result.getStatus(); Log.d(TAG, "onResult(...)" + s.isSuccess()); if (!s.isSuccess()) { Log.d(TAG, "statuskode = " + s.getStatusCode() + " noget gik galt, sandsynlighvis blev gps slået fra = statuscode 1000"); } } private GeofencingRequest createGeoFences() { return new GeofencingRequest.Builder() .addGeofence(geoFenceBuilder(Constants.LOCATION_BALLERUP, "ballerup_req_id")) .addGeofence(geoFenceBuilder(Constants.LOCATION_AALBORG, "aalborg_req_id")) .addGeofence(geoFenceBuilder(Constants.LOCATION_ESBJERG, "esbjerg_req_id")) .addGeofence(geoFenceBuilder(Constants.LOCATION_ÅRHUS, "århus_req_id")) // .addGeofence(geoFenceBuilder(Constants.LOCATION_TAASTRUP, 44)) .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_EXIT) .build(); } private Geofence geoFenceBuilder(LatLng location, String requestId) { return new Geofence.Builder().setCircularRegion(location.latitude, location.longitude, TARGET_RADIUS) .setExpirationDuration(Geofence.NEVER_EXPIRE) .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT) .setRequestId(requestId) .setNotificationResponsiveness(NOTIFICATION_DELAY) .build(); } 

Esto podría ser un error extraño en la parte de Google Geofence APIs. Además, es un poco extraño tener los eventos ENTER y EXIT como disparadores iniciales. Aquí hay una solución fácil para llamar dinámicamente setInitialTrigger() , y evitar esto por completo. Antes de llamar a createGeoFences() , obtenga un punto GPS y compruebe la distancia desde ese punto hasta el punto central de la geofencia. Si se encuentra fuera de la geofence, pase un valor para establecer el desencadenador inicial en GEOFENCE_TRANSITION_ENTER , de lo contrario, GEOFENCE_TRANSITION_EXIT en GEOFENCE_TRANSITION_EXIT .

En cuanto a por qué está haciendo esto, aquí está mi pensamiento en él, aunque no lo he probado. Google puede estar recibiendo la última ubicación conocida, que podría estar fuera de la geofence inicialmente, registrando así el evento GEOFENCE_TRANSITION_EXIT . Entonces comenzará su pila de la localización, considera que el teléfono está realmente dentro del geofence, después registra el acontecimiento de GEOFENCE_TRANSITION_ENTER .

Necesita registrar un IntentService que pueda manejar los disparadores

 public class GeofenceTransitionsIntentService extends IntentService { ... protected void onHandleIntent(Intent intent) { GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); // Get the transition type. int geofenceTransition = geofencingEvent.getGeofenceTransition(); // Test that the reported transition was of interest. if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ) { //TODO: on Transition Enter }else if(geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT){ //TODO: on Transition Exit }else{ Log.e(GEOFENCE, "Event not handled", geofenceTransition)); } 

Explicado aquí: http://developer.android.com/training/location/geofencing.html

Si usted necesita saber si usted está dentro de un geofence usted podría hacer un bucle de la lista actual de geofence ubicaciones y comprobar si uno de ellos es cerca de suficiente para disparar?

Algo como esto:

 public Location insideGeofenceLocation(Location currentLocation, ArrayList<Location> listOfGeofenceLocations, float triggerRadius) { for (Location geoFenceLocation : listOfGeofenceLocations) { if (currentLocation.distanceTo(geoFenceLocation) < triggerRadius) return geoFenceLocation; } return null; } 

Comprobar la ubicación actual:

 Location geofenceLocation = insideGeofenceLocation(locationClient.getLastLocation(), listOfGeofenceLocations, TARGET_RADIUS); if(geofenceLocation!=null){ // TODO: update } 
  • ¿Por qué se ignora el parámetro de consulta `sll` para una URL de Maps (tipo de)?
  • Android - ¿Cómo obtener la ubicación actual (latitud y longitud)?
  • El geocodificador no siempre devuelve un valor
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.