LocationClient的问题

时间:2013-11-06 05:19:15

标签: android location google-play-services location-client

我正在开发一个基于位置的应用程序,我在其中编写了一个服务,该服务使用LocationClient请求频繁的位置更新。当我的数据包打开时,应用程序显示正确的当前位置,但是当我切换到WiFi时,应用程序显示错误的位置(更确切地说是我的旧位置之一)。

我在离开办公室时启动了数据包(无WiFi)的应用程序。在我到达办公室之前,它显示了我正确的位置。到达办公室后,我切换到WiFi。当我切换到WiFi时,它开始显示我的家作为我当前的位置,尽管我在办公室。再次切换到数据包显示我的正确位置。

有没有人遇到与LocationClient类似的问题?自从过去两天以来,我一直在努力解决这个问题,但找不到任何解决方案或理由导致这种奇怪的LocationClient行为。真的很感激任何帮助。

修改 我的位置服务

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

}

1 个答案:

答案 0 :(得分:1)

您可以将优先级设置为PRIORITY_HIGH_ACCURACY,但我猜它会耗尽电量  很快。

mLocRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

PRIORITY_HIGH_ACCURACY更有可能使用GPS,PRIORITY_BALANCED_POWER_ACCURACY更有可能使用WIFI&细胞塔定位。如果您使用WIFI / Cell Tower位置,则必须承担一些近似不准确的结果。

将这两项权限添加到您的清单中,我猜您可能会获得更多改进的结果。

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


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