0

I am developing a location based application where I have written a service that requests frequent location updates using LocationClient. The application is showing correct current location when my data pack is on but as soon as I switch to WiFi, the application is showing wrong location(to be more precise one of my old locations).

I started the application with data pack on(no WiFi) while leaving for my office. It was showing my correct location till I reached office. After reaching office, I switched to WiFi. When I switched to WiFi, it started showing my home as my current location inspite of me being in office. Switching to data pack again showed my correct location.

Has anyone faced similar issues with LocationClient? I have been struggling with this issue since past two days but could not find any solution or reason for such weird behaviour of LocationClient. Any help would really be appreciated.

Edit My Location Service

public class LocationService extends Service implements LocationListener,
    ConnectionCallbacks, OnConnectionFailedListener {

private LocationClient mLocClient;
private LocationRequest mLocRequest;
private IBinder mLocationServiceBinder = new LocationServiceBinder();
private Location mLocation;

private boolean mInProgress = false;
private final static int INTERVAL =  1 * 60 * 1000;
private final static int FASTEST_INTERVAL = 30 * 1000;
private final static int MINIMUM_DISTANCE = 100;

@Override
public void onCreate() {
    super.onCreate();
    mInProgress = false;

    mLocRequest = LocationRequest.create();
    mLocRequest.setInterval(INTERVAL);
    mLocRequest.setFastestInterval(FASTEST_INTERVAL);
    mLocRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    mLocRequest.setSmallestDisplacement(MINIMUM_DISTANCE);

    mLocClient = new LocationClient(this, this, this);

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(getApplicationContext(), "Service started",
            Toast.LENGTH_SHORT).show();
    setUpLocationClientIfNeeded();

    if (!mLocClient.isConnected() || !mLocClient.isConnecting()
            && !mInProgress) {
        mInProgress = true;
        mLocClient.connect();
    }

    return Service.START_STICKY;
}

private void setUpLocationClientIfNeeded() {
    if (mLocClient == null)
        mLocClient = new LocationClient(this, this, this);
}

@Override
public IBinder onBind(Intent intent) {
    return mLocationServiceBinder;
}

@Override
public void onDestroy() {
     mInProgress = false;
     if(mLocClient != null && mLocClient.isConnected()) {
         mLocClient.removeLocationUpdates(this);
         mLocClient.disconnect();
     }

    super.onDestroy();
}

@Override
public void onLocationChanged(Location location) {
    //sending a broadcast to activity on location changed

}

// Create a binder class for communication with activity
public class LocationServiceBinder extends Binder {
    public LocationService getService() {
        return LocationService.this;
    }

}

@Override
public void onConnectionFailed(ConnectionResult result) {
    mInProgress = false;
}

@Override
public void onConnected(Bundle connectionHint) {

    Log.i("Location Client", "Connected");

    mLocClient.requestLocationUpdates(mLocRequest, this);
    mLocation = mLocClient.getLastLocation();

    queryParseForDealsAroundLocation(mLocation);
}

@Override
public void onDisconnected() {
    mInProgress = false;
    mLocClient = null;
    Toast.makeText(getApplicationContext(), "Location Client disconnected",
            Toast.LENGTH_SHORT).show();
    Log.i("Location Client : ", "Disconnected");
}

}

Raeesaa
  • 3,267
  • 2
  • 22
  • 44
  • Would you mind posting some code sir? Aren't you using GPS to get accurate results. Request Location update from GPS. – Yauraw Gadav Nov 06 '13 at 05:28
  • I am using LocationClient which is a part of [new location API](https://developer.android.com/google/play-services/location.html) for android and it uses fused location provider. We don't need to specify GPS_PROVIDER or NETWORK_PROVIDER as in case of LocationManager. But I have turned on all the location services in settings. – Raeesaa Nov 06 '13 at 05:35
  • Added code. I don't think it was needed for this question but still on your request. – Raeesaa Nov 06 '13 at 05:48
  • I also have this problem with the new location client. When the GPS is not available then I receive a WLAN position with wrong location. Did you manage to solve that issue somehow? It seems we have to use the good old location manager. Please check also this discussion: http://stackoverflow.com/questions/18916273/locationclient-vs-locationmanager – Blehi Mar 27 '14 at 07:32

1 Answers1

1

You could set priority to PRIORITY_HIGH_ACCURACY but I guess it will drain the battery quite fast.

mLocRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

PRIORITY_HIGH_ACCURACY is more likely to use GPS, and PRIORITY_BALANCED_POWER_ACCURACY is more likely to use WIFI & Cell tower positioning. If you use WIFI/Cell tower location, you will have to bear with some approximately inaccurate results.

Add both of these permissions to your manifest, I guess you may get more improved results.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Yauraw Gadav
  • 1,706
  • 1
  • 18
  • 39
  • My priority was set to PRIORITY_HIGH_ACCURACY till yesterday. Switched to PRIORITY_BALANCED_POWER_ACCURACY today. Was having same issue with high accuracy as well. As far as I know WiFi gives more accurate location as compared to mobile networks, this weird behaviour is making me mad. – Raeesaa Nov 06 '13 at 05:58