【问题标题】:Location only refreshes when I turn GPS off仅当我关闭 GPS 时位置才会刷新
【发布时间】:2014-06-05 08:48:43
【问题描述】:

所以我在尝试解析我的位置时遇到了问题。当给出查找我的位置的命令时,它反而给了我关闭 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);   
}

【问题讨论】:

    标签: android gps


    【解决方案1】:

    您不能只是重复地向位置管理器查询其最后一个已知位置,这只会为您提供最后一个已知位置。听起来您正在尝试做的是在您四处走动时获取位置更新。

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

    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。只是不要覆盖它。

    【讨论】:

    • 对不起,我没有发布完整的代码。我正在编写的类目前实现了 LocationListener 并覆盖了 onLocationChanged 方法,但没有对它做任何事情。我的帖子现在更新了完整的代码。我在类中编写了一个 Asynctask,我想将 Lat 和 Long 从 onLocationChanged 传递到,但它需要一个上下文。我怎么能把它传进去?谢谢!
    • Service 是一个上下文,您可以像以前一样使用thisthis.getApplicationContext() 可能会更好)。我将通过进一步的建议修改我的答案。
    • 查看 OP 中的编辑,将尝试该代码。感谢您迄今为止的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    相关资源
    最近更新 更多