【问题标题】:Android: GPS is giving last saved locationAndroid:GPS 提供上次保存的位置
【发布时间】:2014-11-11 09:03:56
【问题描述】:

我正在努力通过 GPS 获取当前位置。但 GPS 总是给我最后保存的位置! 可能是因为这条线!我正在寻找更好的解决方案!

位置管理器 .getLastKnownLocation(LocationManager.GPS_PROVIDER);

这里是示例代码

if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }

提前致谢!

【问题讨论】:

  • 是的,因为您使用的是返回最后一个已知位置的 getLastKnownLocation
  • Pramod,要从 LocationManager 获取位置,只有 'locationManager.getLastKnownLocation()' 存在。 Andi,您可以使用“LocationManager.NETWORK_PROVIDER”代替“LocationManager.GPS_PROVIDER”。
  • @NikitaShah 我也试过 LocationManager.NETWORK_PROVIDER ......但这也没有用。

标签: android gps


【解决方案1】:

检查此代码会对您有所帮助。

// 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
String address; //Address

// 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 network provider is enabled
            } else {
                this.canGetLocation = true;
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }

【讨论】:

    【解决方案2】:

    如果你使用

    locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER);
    

    它只会返回 GPS 内部保存的最后一个位置。

    由于您已经添加了一个侦听器,因此您可以在 onLocationChanged 覆盖的函数上获取下一个更改的位置,这将是您需要的最近位置。

    没有办法,你需要等到onLocationChanged函数自动触发...

    【讨论】:

      【解决方案3】:

      简单的实现是你需要一个位置监听器

      别忘了添加权限

      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
      <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
      

      您可以在下面的监听器中获得位置变化的频繁更新。

      private final LocationListener mLocationListener = new LocationListener() {
          @Override
          public void onLocationChanged(final Location location) {
              //your code here
                           if (location != null) {
                              latitude = location.getLatitude();
                              longitude = location.getLongitude();
                          }
          }
      };
      

      你必须在创建时注册它

          protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
      
                  mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
      
                  mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,    LOCATION_REFRESH_TIME,
                          LOCATION_REFRESH_DISTANCE, mLocationListener);
                     //first time you can take the value from the lastKnown location 
                     if (locationManager != null) {
                     Location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                              if (location != null) {
                                  latitude = location.getLatitude();
                                  longitude = location.getLongitude();
                              }
                          }
      
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-06
        • 2014-03-10
        • 1970-01-01
        • 2018-04-20
        相关资源
        最近更新 更多