Android LocationManager标准

Android LocationManager标准,第1张

概述我需要从网络和GPS提供商处接收位置更改. 如果GPS提供商不可用或没有位置(卫星可见性差)我会从网络提供商处获得GPS提供商的位置. 是否有可能根据我的需要选择使用标准? 实际上 Android Developers – Making Your App Location Aware有一个很好的示例代码来满足您的需求. 在其代码中,如果您使用两个提供商(来自GPS和网络),它将进行比较: ... 我需要从网络和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标准全部内容,希望文章能够帮你解决Android LocationManager标准所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1138978.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存