当我关闭GPS时,位置只会刷新

时间:2014-06-05 00:10:18

标签: android gps

所以我在尝试解决我的位置时遇到问题。当给出命令来查找我的位置时,它会给我关闭GPS的最后位置。能够使用WiFi找到我的粗略位置似乎也无法正常工作。

这是我目前的课程

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; //10 metters

//The minimum time beetwen updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

//Declaring a Location Manager
protected LocationManager locationManager;



public void fetchLocation(Context context) {
    contextnormal = context;
    getLocation(context);
    if (canGetLocation())
    {
        String stringLatitude = String.valueOf(latitude);
        String stringLongitude = String.valueOf(longitude);
        Log.i("Location: ", 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;

            //First get location from Network Provider
            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);
                    updateGPSCoordinates();
                }
            }

            //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();
                    }
                }
            }
        }
    }
    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) 
{   
     //Want to Execute Asynctask method to HTTP post the lat and long. Is there a way `to pass in a context to do this?`
}

@Override
public void onProviderDisabled(String provider) 
{   
}

@Override
public void onProviderEnabled(String provider) 
{   
    if (contextnormal != null) {
        fetchLocation(contextnormal);
    }
}

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

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

我猜这个问题就在于这些问题

  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();
                            }
                        }

由于某种原因,它将最后一次已知的位置作为GPS打开的最后时间。如果GPS开启的话,有什么想法,而不是将最后的已知位置作为当前位置?

编辑:

实施以下方法并进行测试:

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

1 个答案:

答案 0 :(得分:0)

您不能只重复查询位置管理器的最后一个已知位置,这样只能获取最后一个已知位置。这听起来像你要做的是在你四处走动时获得位置更新。

在这种情况下,您需要注册位置更新,就像您尝试使用线路一样:

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

你在这里是正确的轨道,但是什么是this(函数调用的最后一个参数[是的我知道它是对当前类的引用...])?最后一个参数应该是一个意图或回调,其中将更新更新。您似乎没有从您提供的代码中实现它。

this参数更改为实现LocationListener的类。位置更新将转到onLocationChanged方法,然后您可以根据需要处理它们。一个简单的方法是添加一个实现监听器的嵌套内部类,然后您可以创建该类的实例并将其传递给位置管理器,代替您当前的this参数。

修改

好的,所以你正确地实现了这个东西,但是盲目地忽略了位置更新,不要这样做。您拥有的服务 Context - 所以您可以根据需要传递它,尽管使用它可能更好:

this.getApplicationContext();

如果你将它传递给一些长时间运行的东西。我建议您在到达onLocationChanged()时对您的位置更新进行排队。排队该位置后,发出后台工作线程(或AsyncTask)的信号,以实际将该位置发布到您的Web服务。

此外,请勿覆盖onBind并返回null。只是不要覆盖它。