【问题标题】:Issues with LocationClientLocationClient 的问题
【发布时间】:2013-11-06 05:19:15
【问题描述】:

我正在开发一个基于位置的应用程序,我在其中编写了一个使用 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");
}

}

【问题讨论】:

  • 您介意发布一些代码吗?您不是使用 GPS 来获得准确的结果吗?从 GPS 请求位置更新。
  • 我正在使用 LocationClient,它是 Android 的 new location API 的一部分,它使用融合的位置提供程序。我们不需要像 LocationManager 那样指定 GPS_PROVIDER 或 NETWORK_PROVIDER。但是我已经在设置中开启了所有的定位服务。
  • 添加了代码。我认为这个问题不需要它,但仍应您的要求。
  • 新定位客户端也有这个问题。当 GPS 不可用时,我会收到位置错误的 WLAN 位置。您是否设法以某种方式解决了该问题?看来我们必须使用好的旧位置管理器。另请查看此讨论:stackoverflow.com/questions/18916273/…

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


【解决方案1】:

您可以将优先级设置为 PRIORITY_HIGH_ACCURACY 但我想它会耗尽电池电量 相当快。

mLocRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

PRIORITY_HIGH_ACCURACY 更有可能使用 GPS,而 PRIORITY_BALANCED_POWER_ACCURACY 更有可能使用 WIFI & Cell tower 定位。如果您使用 WIFI/手机信号塔定位,您将不得不忍受一些近似不准确的结果。

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

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


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

【讨论】:

  • 我的优先级设置为 PRIORITY_HIGH_ACCURACY 直到昨天。今天切换到 PRIORITY_BALANCED_POWER_ACCURACY。也有同样的问题,精度也很高。据我所知,与移动网络相比,WiFi 提供了更准确的位置,这种奇怪的行为让我很生气。
猜你喜欢
  • 2013-09-25
  • 1970-01-01
  • 1970-01-01
  • 2013-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多