4

In my method for building a geofence, I am getting an error on ExpirationDuration(NEVER_EXPIRE) and setTransitionTypes(GEOFENCE_TRANSITION_ENTER) stating that they can't be resolved to a variable. Why is this happening?

My method:

private void buildGeofence(){
    LatLng geofencePoint = marker.getPosition();
    int radius = 1610;
    Geofence.Builder geofence = new Geofence.Builder();
    geofence.setCircularRegion(geofencePoint.latitude,geofencePoint.longitude, radius);
    geofence.setExpirationDuration(NEVER_EXPIRE);
    geofence.setTransitionTypes(GEOFENCE_TRANSITION_ENTER);
    geofence.setNotificationResponsiveness(0);
    geofence.build();
}
Community
  • 1
  • 1
user3391426
  • 433
  • 1
  • 5
  • 17

2 Answers2

9

Those constants are already declared in the Geofence Class you reference, so jut use them as geofence.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER);

Stefan Reimers
  • 379
  • 4
  • 9
-3

Yeah, so I forgot to declare the constants. Duh! Here's the declarations I used:

long NEVER_EXPIRE = -1;
int GEOFENCE_TRANSITION_ENTER = 1;

More on the matter here: https://developer.android.com/reference/com/google/android/gms/location/Geofence.html

user3391426
  • 433
  • 1
  • 5
  • 17
  • 4
    You should use the constants defined in Geofence (as @stefan has stated) and not define your own.Your app could break if the values of the constants were updated in a later version of Android. – mcnicholls Sep 10 '14 at 09:22
  • You don't need to declare the constants because they're already declared in the library, but you do need to reference the library constants in a way that java can tell what you're referencing. Presumably you have `import com.google.android.gms.location.Geofence;` at the top, so you can reference the library's constants as `Geofence.NEVER_EXPIRE` and `Geofence.GEOFENCE_TRANSITION_ENTER` – Niall Jun 08 '23 at 20:27