5

I have a problem in the accuracy of geofences because user's location is mostly determined by mobile networks which sucks completely.

My app uses geofences around selected places that will perform some action when the user enters these places...I confirm that my app actually works because when I open google maps the accuracy of location increases and then my geofence is triggered.

My current solution is using location manager with the gps provider that gets location every one minute but it seems to affect battery life and it stops after sometime.

My question is: is there a way to make geofences accurate to a 60 m radius for example? Or is there any way to get user's precise location updates?

Thanks in advance

Turki Alkhateeb
  • 100
  • 1
  • 9
  • 1
    You can use GSM & GPS data together to boost up your location detection performance. A good example of detecting location by GPS could be found here, http://stackoverflow.com/questions/6181704/good-way-of-getting-the-users-location-in-android – Prokash Sarkar Aug 02 '15 at 03:59
  • Thank you but my app requires geofences to be triggered instantly. I dont need to get location every 30 minutes or so – Turki Alkhateeb Aug 02 '15 at 04:11
  • `is there a way to make geofences accurate to a 60 mO`. The question is wrong as it depends on the precision of the location provider only. Gps chips in smartphones are not that good. But 60 meter is bad. Many phones will do 15 m or less. – greenapps Aug 02 '15 at 15:35

1 Answers1

2

You will have better idea about accuracy by following this thread :

How to make Geo Fencing alert more accurate in Android

Once you create your geofence, you can start to ping your GPS until you will get ENTER transition. I tried this way and I got better accuracy.

Simple make service to ping GPS :

public class GPSService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

GoogleApiClient mGoogleApiClient;
LocationRequest locationRequest;

public GPSService() {
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
    super.onCreate();


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

    mGoogleApiClient.connect();


}

@Override
public void onLocationChanged(Location location) {


    Utility.ReadAndWriteData(this, Utility.readFileName(this), "Still Geofence is not triggered!!!");

}

@Override
public void onConnected(Bundle bundle) {

    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setFastestInterval(1000);
    locationRequest.setInterval(2000);
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,locationRequest,this);

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onDestroy() {
    super.onDestroy();

    if(mGoogleApiClient.isConnected()) {

        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

    }
} 
Community
  • 1
  • 1
Jai
  • 1,974
  • 2
  • 22
  • 42
  • My app always runs in the background so I think polling gps always affects battery life as I described above...I need to know how to ping the gps exactly, should I do it using location manager? What min time and min distance should I set? – Turki Alkhateeb Aug 02 '15 at 04:01
  • @Dev : Actually you need to ping GPS only untill your current geofence will trigger with ENTER transition. So when you will write code to create geofence after creating it, you will start pinging your GPS. Once you will get ENTER transition you have to stop pinging it. That's it. As you have mentioned, 60M radius is very less in case of geofence, so if you can increase make it at least 500 M, So you will have better result because within less radius it was unable to track transiiton – Jai Aug 02 '15 at 04:20
  • @Dev : Added simple service call above – Jai Aug 02 '15 at 04:23
  • What if the user entered another geofence? The app won't work in that case bacause I register 20 geofences that is nearby the user's location – Turki Alkhateeb Aug 02 '15 at 04:24
  • Also, will setting a bigger radius will trigger enter and exit transitions correctly? – Turki Alkhateeb Aug 02 '15 at 04:26
  • @Dev : You can track each geofence using Geofence ID right? While creating geofence whatever the unique ID you are passing, you can check same ID when each geofence will have ENTER transition. – Jai Aug 02 '15 at 04:26
  • @Dev : Yeah while making bigger radius, you will have better result about transitions – Jai Aug 02 '15 at 04:27
  • I just saw your edit and thanks man I think that will work! I will be back tommorow and I will tell you the results, thank you very much – Turki Alkhateeb Aug 02 '15 at 04:31
  • One last question, I prefer to start services instead of binding to them...will this service be ever killed? Because I was using location manager with GPS provider and it was getting killed after sometime – Turki Alkhateeb Aug 02 '15 at 04:33
  • @Dev : Use FusedLocationAPI instead of location manager as you just need to take lastknownlocation. And yeah that's why I used to ping using service only so you can easily start and stop whenever you want. – Jai Aug 02 '15 at 04:40
  • 1
    Hello @Jai, I'm going to try this today :) – Rahul Upadhyay Oct 11 '17 at 06:50
  • @RahulUpadhyay : Cool you'll have best experience :) Let me know afterwards! – Jai Oct 11 '17 at 08:04