簡體   English   中英

如何在執行期間更改LocationManager提供程序

[英]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服務)

融合位置提供程序可以智能地管理基礎的位置技術,並根據您的需求為您提供最佳位置。

  • 簡單的API:可讓您指定“高精度”或“低功耗”之類的高層需求,而不必擔心位置提供程序。
  • 立即可用:使您的應用可以立即訪問最近的最佳位置。
  • 節能:最大限度地減少應用程序的功耗。 基於所有傳入的位置請求和可用的傳感器,融合的位置提供者選擇最有效的方式來滿足這些需求。
  • 多功能性:滿足廣泛的需求,從需要高度精確位置的前景用途到需要定期位置更新且功耗影響可忽略的背景用途。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM