使用旧位置的Android GPS

时间:2014-07-06 02:21:59

标签: android gps

我试图了解有关Android中位置服务的更多信息,并尝试构建一个可以找到Android设备并将其经度和经度发送到服务器的应用程序。我已经有一段时间按预期工作,但我仍然被一个小虫子困扰。当我从服务器发送命令以便第一次找到设备时,设备会返回一个最近但很旧的位置,例如我在同一天驾驶的道路。

设备第二次从服务器接收命令以找到设备时,设备返回准确的位置。

以下是相关代码:

LocationTracker.java
public class LocationTracker extends Service implements LocationListener {



    //flag for GPS Status
    boolean isGPSEnabled = false;

    //flag for network status
    boolean isNetworkEnabled = false;

    boolean canGetLocation = false;
    Location location;
    double latitude;
    double longitude;

    //The minimum distance to change updates in metters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10*1000; //10,000 meters

    //The minimum time beetwen updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 10000; // 10,000 minutes

    //Declaring a Location Manager
    protected LocationManager locationManager;



    public void fetchLocation(Context context) {
        getLocation(context);
        if (canGetLocation())
        {
            String stringLatitude = String.valueOf(latitude);
            String stringLongitude = String.valueOf(longitude);
            Log.i("Location: ", stringLatitude + " " + stringLongitude);
            new MyAsyncTask().execute(stringLatitude, stringLongitude);     
        }
        else
        {
            // can't get location
            // GPS or Network is not enabled
            // Ask user to enable GPS/network in settings
            Log.i("Error: ", "Cannot get location");
        }
    }


    public Location getLocation(Context context)
    {
        try
        {
            locationManager = (LocationManager) context.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 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);
                            updateGPSCoordinates();
                        }
                    }
                }

                //If no GPS, get location from Network Provider
                if (isNetworkEnabled && !isGPSEnabled)
                {
                    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);
                        updateGPSCoordinates();
                    }
                }                


            }
        }
        catch (Exception e)
        {
            //e.printStackTrace();
            Log.e("Error : Location", "Impossible to connect to LocationManager", e);
        }

        return location;
    }

    public void updateGPSCoordinates()
    {
        if (location != null)
        {
            latitude = location.getLatitude();
            longitude = location.getLongitude();
        }
    }

    /**
     * Stop using GPS listener
     * Calling this function will stop using GPS in your app
     */

    public void stopUsingGPS()
    {
        if (locationManager != null)
        {
            locationManager.removeUpdates(LocationTracker.this);
        }
    }

    /**
     * Function to get latitude
     */
    public double getLatitude()
    {
        if (location != null)
        {
            latitude = location.getLatitude();
        }

        return latitude;
    }

    /**
     * Function to get longitude
     */
    public double getLongitude()
    {
        if (location != null)
        {
            longitude = location.getLongitude();
        }

        return longitude;
    }

    /**
     * Function to check GPS/wifi enabled
     */
    public boolean canGetLocation()
    {
        return this.canGetLocation;
    }

    @Override
    public void onLocationChanged(Location location) 
    {   
        double newLat = location.getLatitude();
        double newLong = location.getLongitude(); 
        String stringNewLatitude = String.valueOf(newLat);
        String stringNewLongitude = String.valueOf(newLong);
        Log.i("New Location: ", stringNewLatitude + " " + stringNewLongitude);
        new MyAsyncTask().execute(stringNewLatitude, stringNewLongitude);   
    }

    @Override
    public void onProviderDisabled(String provider) 
    {   
    }

    @Override
    public void onProviderEnabled(String provider) 
    {   

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) 
    {   
    }

    public IBinder onBind(Intent intent) 
    {
        return null;
    }

为什么我的位置在第一次尝试时更新为旧位置,第二次更新为正确位置?

另请注意,我还想删除此处显示的requestLocationUpdates:

locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

因为它导致死线程警告的处理程序,但当我删除它时,我的设备停止获取我的位置。这可能是问题的一部分。

我非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

这是因为您正在使用getLastKnownLocation()

location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

getLastKnownLocation(String Provider): 返回一个位置,指示从给定提供程序获取的上次已知位置修复的数据。

这可以在不启动提供程序的情况下完成。请注意,此位置可能已过期,例如,如果设备已关闭并移至其他位置。