【问题标题】:Fused Location Provider Api get location from Gps when wifi is offwifi 关闭时,融合位置提供程序 Api 从 Gps 获取位置
【发布时间】:2016-12-13 07:43:58
【问题描述】:

我已经在我的应用中实现了 Google Play 服务的 Fused Location Api,以不断获取用户位置。 wifi开启时一切正常

但是当我关闭 wifi 时,它会使用蜂窝信息更新用户位置,因此准确度超过 700 米。

我想要的是当 wifi 关闭时,然后从 GPS 获取用户位置。

 protected synchronized void buildGoogleApiClient() {
    Log.i(TAG, "Building GoogleApiClient");
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    createLocationRequest();
}

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();


    mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);


    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);

    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

 @Override
public void onConnected(Bundle connectionHint) {
    Log.i(TAG, "Connected to GoogleApiClient");


    if (mCurrentLocation == null) {
        mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
        updateUI();
    }

    if (mRequestingLocationUpdates) {
        startLocationUpdates();
    }
}


@Override
public void onLocationChanged(Location location) {
    mCurrentLocation = location;
    mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
    updateUI();
    Toast.makeText(this, getResources().getString(R.string.location_updated_message),
            Toast.LENGTH_SHORT).show();
}

【问题讨论】:

  • 将此添加到您的 mLocationRequest mLocationRequest.setSmallestDisplacement(5);
  • 这只会设置 5m 位移进行更新。
  • 这将提高您的准确性,因为如果用户位置变化超过 5 米,它将触发位置更新

标签: android android-gps android-fusedlocation


【解决方案1】:

使用位置服务的应用必须请求位置权限。 Android 提供两种位置权限:ACCESS_COARSE_LOCATION 和 ACCESS_FINE_LOCATION。您选择的权限决定了 API 返回的位置的准确性。

换句话说,您选择的权限决定了将用于确定位置的提供程序,然后也会影响准确性,如下所示: ACCESS_FINE_LOCATION -> Gps, Cell, Wifi ACCESS_COARSE_LOCATION -> 仅限手机和 Wifi

准确性取决于提供者。但结合优先 LocationRequest.PRIORITY_HIGH_ACCURACY -> 您的应用可能会使用 GPS 来确定您的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 2013-08-02
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    相关资源
    最近更新 更多