Android LocationManager 标准

标签 android location criteria locationmanager

我需要从网络和 GPS 提供商那里接收位置更改。

如果 GPS 供应商不可用或没有定位(卫星可见度差),我会从网络供应商处接收位置信息,否则从 GPS 供应商处接收位置信息。

是否可以根据我的需要使用标准来选择提供者?

最佳答案

实际上 Android Developers - Making Your App Location Aware有很好的示例代码可以满足您的需求。

在其代码中,如果您同时使用两个提供商(来自 GPS 和网络),它将进行比较:

...
} else if (mUseBoth) {
    // Get coarse and fine location updates.
    mFineProviderButton.setBackgroundResource(R.drawable.button_inactive);
    mBothProviderButton.setBackgroundResource(R.drawable.button_active);
    // Request updates from both fine (gps) and coarse (network) providers.
    gpsLocation = requestUpdatesFromProvider(
            LocationManager.GPS_PROVIDER, R.string.not_support_gps);
    networkLocation = requestUpdatesFromProvider(
            LocationManager.NETWORK_PROVIDER, R.string.not_support_network);

    // If both providers return last known locations, compare the two and use the better
    // one to update the UI.  If only one provider returns a location, use it.
    if (gpsLocation != null && networkLocation != null) {
        updateUILocation(getBetterLocation(gpsLocation, networkLocation));
    } else if (gpsLocation != null) {
        updateUILocation(gpsLocation);
    } else if (networkLocation != null) {
        updateUILocation(networkLocation);
    }
}
...

它实现了最佳位置提供者的想法(通过确定指定时间段内的准确性,例如:

/** Determines whether one Location reading is better than the current Location fix.
  * Code taken from
  * http://developer.android.com/guide/topics/location/obtaining-user-location.html
  *
  * @param newLocation  The new Location that you want to evaluate
  * @param currentBestLocation  The current Location fix, to which you want to compare the new
  *        one
  * @return The better Location object based on recency and accuracy.
  */
protected Location getBetterLocation(Location newLocation, Location currentBestLocation) {
    if (currentBestLocation == null) {
        // A new location is always better than no location
        return newLocation;
    }

    // Check whether the new location fix is newer or older
    long timeDelta = newLocation.getTime() - currentBestLocation.getTime();
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
    boolean isNewer = timeDelta > 0;

    // If it's been more than two minutes since the current location, use the new location
    // because the user has likely moved.
    if (isSignificantlyNewer) {
        return newLocation;
    // If the new location is more than two minutes older, it must be worse
    } else if (isSignificantlyOlder) {
        return currentBestLocation;
    }

    // Check whether the new location fix is more or less accurate
    int accuracyDelta = (int) (newLocation.getAccuracy() - currentBestLocation.getAccuracy());
    boolean isLessAccurate = accuracyDelta > 0;
    boolean isMoreAccurate = accuracyDelta < 0;
    boolean isSignificantlyLessAccurate = accuracyDelta > 200;

    // Check if the old and new location are from the same provider
    boolean isFromSameProvider = isSameProvider(newLocation.getProvider(),
            currentBestLocation.getProvider());

    // Determine location quality using a combination of timeliness and accuracy
    if (isMoreAccurate) {
        return newLocation;
    } else if (isNewer && !isLessAccurate) {
        return newLocation;
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
        return newLocation;
    }
    return currentBestLocation;
}

有关完整代码,请查看上面提供的链接。

关于Android LocationManager 标准,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15679205/

相关文章:

android - 如何让地理围栏精准?

Android RequestLocationUpdates 到带有额外包的 pendingItent

iphone - iOS:使用 CLLocationManager 获得准确的反向地理编码?

用于选择具有空子集合的所有实体的 nHibernate 标准

java - SQLITE 文件大小小?

android - : "Android Library Update" 期间发生内部错误

android - Google Play 应用签名 - KeyHash 不匹配

java - 应用程序在启动时崩溃,尝试查看登录屏幕是否存在文件

java - 在java标准谓词中使用 native sql

java - Hibernate Criteria API 等同于 HQL select 子句?