11

I've gone through the tutorial for Geofencing and I have question in mind.

Does geofencing require you to continuously monitor the user's location ?

Or will the LocationClient to which you add the Geofence automatically trigger when a transition has orccured ?

This might seem like an obvious question, but the tutorial never mentioned anything about requesting location updates from the location manager.

Traxex1909
  • 2,650
  • 4
  • 20
  • 25

2 Answers2

3

Does geofencing require you to continuously monitor the user's location ? - NO, it doesn't require you to monitor location.

Only requirements are

  1. Register your Geofence (one time action)
  2. Location Adapter should be on (System level setting, App can trigger dialog to turn on location adapter if disabled)
  3. Google Play services will automatically trigger actions and app will receive call back

Or will the LocationClient to which you add the Geofence automatically trigger when a transition has occurred ? - YES It will be automatically triggered. Make sure you set the right expiry timestamp and location adapter is on. App doesn't need to know the current location

[Optional]

Also, you can catch the right geofence errors to know, why didn't your geo-fence get triggered:

GEOFENCE_NOT_AVAILABLE Geofence service is not available now. Typically this is because the user turned off location access in settings > location access.

Constant Value: 1000

GEOFENCE_TOO_MANY_GEOFENCES Your app has registered more than 100 geofences. Remove unused ones before adding new geofences.

Constant Value: 1001

GEOFENCE_TOO_MANY_PENDING_INTENTS You have provided more than 5 different PendingIntents to the addGeofences(GoogleApiClient, GeofencingRequest, PendingIntent) call.

Constant Value: 1002

Mayuri Khinvasara
  • 1,437
  • 1
  • 16
  • 12
  • will it work if the app is in background or only it will work when the app in foreground? – karan Jul 07 '23 at 08:00
-4
        public class MainActivity extends FragmentActivity implements
          OnMarkerDragListener {

         private GoogleMap googleMap;
         private int distance;
         private SupportMapFragment mapFragment;

         @Override
         public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          SupportMapFragment mapFragment = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map));
          googleMap = mapFragment.getMap();
          googleMap.setOnMarkerDragListener(this);
          distance = 100;
          googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
            26.788707, 75.828108), 15));
          createGeofence(26.788707, 75.828108, distance, "CIRCLE", "GEOFENCE");
         }
         private void createGeofence(double latitude, double longitude, int radius,
           String geofenceType, String title) {

          Marker stopMarker = googleMap.addMarker(new MarkerOptions()
            .draggable(true)
            .position(new LatLng(latitude, longitude))
            .title(title)
            .icon(BitmapDescriptorFactory
              .fromResource(R.drawable.stop_marker)));

          googleMap.addCircle(new CircleOptions()
            .center(new LatLng(latitude, longitude)).radius(radius)
            .fillColor(Color.parseColor("#B2A9F6"))); 
         }
         @Override
         public void onMarkerDrag(Marker marker) {
         }
         @Override
         public void onMarkerDragEnd(Marker marker) {
          LatLng dragPosition = marker.getPosition();
          double dragLat = dragPosition.latitude;
          double dragLong = dragPosition.longitude;
          googleMap.clear();
          createGeofence(dragLat, dragLong, distance, "CIRCLE", "GEOFENCE");
          Toast.makeText(
            MainActivity.this,
            "onMarkerDragEnd dragLat :" + dragLat + " dragLong :"
              + dragLong, Toast.LENGTH_SHORT).show();
          Log.i("info", "on drag end :" + dragLat + " dragLong :" + dragLong);

         }
         @Override
         public void onMarkerDragStart(Marker marker) {
         }



   main .xml 

    < ?xml version="1.0" encoding="utf-8"?>
     < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        < fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment" />
    < /RelativeLayout>
Dakshesh Khatri
  • 639
  • 7
  • 12