如何在执行期间更改LocationManager提供程序

时间:2014-05-13 14:22:26

标签: android gps locationmanager android-location

有什么方法可以在运行时切换LocationManager的提供程序吗?

我需要知道每2000米的位置。所以我创建了一个LocationManager并设置如下:

locationManager.requestLocationUpdates(provider, 0, 2000, this);

这个想法是让locationManager保持运行所有应用程序的生命周期。所以我不想"杀死" LocationManager(使用locationManager.removeUpdates(this);)。

如果在应用执行过程中GPS已关闭或没有响应(超时),我正在寻找一种方法来更改网络提供商的GPS提供商

谢谢,对不起我的英文

2 个答案:

答案 0 :(得分:0)

此代码可能对您有所帮助。它的作用是,如果可用,它将采用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;
}

答案 1 :(得分:-1)

或者您可以使用Fused Location Provider(Google Play服务)

融合位置提供商智能地管理底层定位技术,并根据您的需求为您提供最佳位置。

  • 简单的API:允许您指定高级别需求,例如"高精度"或者"低功耗"而不必担心位置提供商。
  • 立即可用:让您的应用可以立即访问最佳的最新位置。
  • 电源效率:最大限度地减少应用对电源的使用。基于所有传入的位置请求和可用的传感器,融合位置提供商选择最有效的方式来满足这些需求。
  • 多功能性:满足广泛的需求,从需要高度精确定位的前景使用到需要定期更新位置的背景使用,而且功率影响可以忽略不计。