1

I am working on Location and Geo Fencing. But I have seen couple of problems. I think that I have missing some thing but indeed I have many confusions regarding Geo Fencing and Location services Such as Fused Location API.

What I am doing

According to my App scenario, I have to get the user location ( which I am getting using Fused Location API) and also I have ask the user what is his destination and let say he is on Point A and he picks Place F. Now I want that My app should notify him that he has Arrived at point F.

Problems and Confusion :

  1. I am notified (through my notification) that I have arrived to my destination only and only if I am in my app. it looks like that my location did not update google to my current location. so what is a best way that if the app is close or in back ground google still track him

  2. What I am doing is creating the geo fence around the destination of user. Now I do not know what is a way of google to track user. ? can you please clear me that after creating geo fence how does google track Us ?

  3. Do I need to implement some thing in service so that my service should remain running if my app is in background or even my app is force closed?

I think I am quiet clear about my question that What I want . Please share your views and also tell me how does google track us when we geo fence any location ?

I am following this to create the geofence.

Please sahre your views.

Community
  • 1
  • 1

1 Answers1

0

You should register a GEOFENCE_TRANSITION_ENTER or GEOFENCE_TRANSITION_DWELL geofence with the target coordinate (Place F).

In your Activity/Fragment onCreate you should create the Api client:

mGoogleApiClient = new GoogleApiClient.Builder(this)
                                      .addConnectionCallbacks(this)
                                      .addOnConnectionFailedListener(this)
                                      .addApi(LocationServices.API)
                                      .build();

Also remember to connect/disconnect:

protected void onStart() {
    mGoogleApiClient.connect();
    super.onStart();
}

protected void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}

Then in onConnected you should do the following:

LocationServices.GeofencingApi.addGeofences(mGoogleApiClient,
                                            geofenceRequest,
                                            pendingIntent)

You should only add the Geofence once.

Where geofenceRequest is built using GeofencingRequest.Builder:

geofenceRequest = new GeofencingRequest.Builder().addGeofence(yourGeofence).build()

Where yourGeofence and pendingIntent:

yourGeofence = new Geofence.Builder()....build(); // Here you have to set the coordinate of Place F and GEOFENCE_TRANSITION_ENTER/GEOFENCE_TRANSITION_DWELL
pendingIntent = PendingIntent.getService(this,
                                         (int)(System.currentTimeMillis()/1000),
                                         new Intent(this, GeofenceTransitionsIntentService.class),
                                         PendingIntent.FLAG_UPDATE_CURRENT);

Where GeofenceTransitionsIntentService could be like:

public class GeofenceTransitionsIntentService extends IntentService {
    @Override
    protected void onHandleIntent(Intent intent) {
        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);

        if (!geofencingEvent.hasError()) {
            int geofenceTransition = geofencingEvent.getGeofenceTransition();
            if (geofenceTransition != -1) {
                List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
                if (triggeringGeofences != null && triggeringGeofences.size() > 0) {
                    Geofence geofence = triggeringGeofences.get(0);
                    // Do something with the geofence, e.g. show a notification using NotificationCompat.Builder
                }
            }
        }
    }
}

Remember to register this service in your manifest:

<service android:name=".GeofenceTransitionsIntentService"/>

GeofenceTransitionsIntentService.onHandleIntent() will be called even if the app is closed.

Hope it helps.

fernandospr
  • 2,976
  • 2
  • 22
  • 43
  • Thanks for the detailed information!! when _mGoogleApiClient.disconnect();_ called GPS tracking stops (we can see it in the notification) So we never know when Google Play service how frequently update the user location, I guess we need to work with GCMnwmanager/Jobschedular api to update the location trigger for every 30seconds or 1min to get the location update to fire Geofence trigger, any suggetions from you? – LOG_TAG Oct 21 '16 at 07:58
  • 1
    I haven't seen any documented way to know or modify the frequency, this is handled by the OS, and I guess you should rely on it. If this is not enough for your use case then I guess you shouldn't use the Geofencing API and do it kind of manually using a background service that should be requesting the user location, beware that this will probably kill your phone battery I guess. – fernandospr Oct 21 '16 at 10:40