Search code examples
androidgeolocationlocationandroid-locationandroid-geofence

How to make geofences accurate?


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


Solution

  • 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);
    
        }
    }