[英]How to change the LocationManager provider during execution
有什么方法可以在運行時切換LocationManager的提供程序?
我需要知道每2000米的位置。 因此,我創建了一個LocationManager並進行如下設置:
locationManager.requestLocationUpdates(provider, 0, 2000, this);
這個想法是讓locationManager在整個應用程序生命周期中保持運行。 所以我不想“殺死” LocationManager(使用locationManager.removeUpdates(this);
)。
我正在尋找一種方法來更改網絡提供商的GPS提供程序,如果在應用程序執行期間GPS關閉或沒有響應(超時)
謝謝,抱歉我的英語
此代碼也許可以為您提供幫助。 它的作用是,它將獲取GPS坐標(如果有)。 如果GPS坐標不可用,它將與網絡坐標一起穩定下來。
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no GPS Provider and no network provider is enabled
}
else
{ // Either GPS provider or network provider is enabled
// First get location from Network Provider
if (isNetworkEnabled)
{
locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null)
{
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
this.canGetLocation = true;
}
}
}// End of IF network enabled
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled)
{
locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null)
{
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
this.canGetLocation = true;
}
}
}// End of if GPS Enabled
}// End of Either GPS provider or network provider is enabled
} catch (Exception e)
{
e.printStackTrace();
}
// If GPS is enabled, the GPS coordinates will be returned in location.
// If GPS is not enabled, then the network coordinates will be returned.
// If both are enabled, then GPS co orindate will be returned because GPS coordinates have more accuracy than network coordinates.
return location;
}
或者,您可以使用融合位置提供程序 (Google Play服務)
融合位置提供程序可以智能地管理基礎的位置技術,並根據您的需求為您提供最佳位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.