Android GPS正在返回过时的信息

时间:2012-11-26 17:19:29

标签: android google-maps gps locationmanager locationlistener

在我当前的应用程序中,返回的位置数据已过时。我使用一种方法,根据更新之间的最短时间/距离获取更新。但是,让我说我关闭手机,开车到另一个城市,然后重新打开手机。在这种情况下,我的GPS读数将关闭。如何强制应用获取当前位置而不是getLastKnownLocation()。

我听说过locationListener,但我读过的所有内容都非常模糊,如何使用它。

以下是我的代码,以便澄清最新情况:

public class GPSHandling extends Service implements LocationListener{

    private final Context myContext;

    //flag for gps status
    public boolean isGPSEnabled = false;

    //flag for network status
    public boolean isNetworkEnabled = false;

    // used to determine if i can get a location either by network or GPS
    public boolean canGetLocation = false;

    Location myloc;

    public double latitude;
    public double longitude;

    public int MIN_TIME_BTWN_UPDATE = 500*10;  // in miliseconds, so 10sec 
    public int MIN_DISTANCE_BTWN_UPDATE = 10;  // in meters 
    protected LocationManager locManager;

    public GPSHandling(Context context){
        this.myContext= context;
        getLocation();
    }
    public Location getLocation(){
        try{
            locManager =(LocationManager) myContext.getSystemService(LOCATION_SERVICE);

            // now get gps status
            isGPSEnabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            //now the same for network
            isNetworkEnabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled){
                // do nothing since neither provider are enabled (this.cangetlocation = false but its already set to that by default)
            }
            else{
                this.canGetLocation=true;
                //first get values for locManager from the network provider. send parameters telling when to update
                if(isNetworkEnabled){
                    locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BTWN_UPDATE, MIN_DISTANCE_BTWN_UPDATE, this);
                    Log.d("provider", "network");
                    //if we are successful, then check to see if the location manager isnt null. attempt to get the current location from the manager
                    if (locManager != null){
                        myloc =locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                            // after getting the current location, attempt to get the latitude and longitude values
                            if (myloc != null){
                                latitude = myloc.getLatitude();
                                longitude = myloc.getLongitude();
                                              }
                                           }
                                    }
                //now get values for locManager from the GPS provider. send parameters telling when to update
                if(isGPSEnabled){
                    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BTWN_UPDATE, MIN_DISTANCE_BTWN_UPDATE, this);
                    Log.d("provider", "GPS");
                }
                //if we are successful, then check to see if the location manager isnt null. attempt to get the current location from the manager
                    if(locManager!= null){
                        myloc = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    }
                    // after getting the current location, attempt to get the latitude and longitude values
                        if(myloc != null){
                            latitude = myloc.getLatitude();
                            longitude = myloc.getLongitude();
                        }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return myloc;
    }
    // get an update of the current latitude by using this class method
    public double getMyLatitude(){
        if (myloc!= null){
            latitude = myloc.getLatitude();
        }
        return latitude;
    }
    //get an update of the current longitude by using this class method
    public double getMyLongitude(){
        if (myloc != null ){
            longitude = myloc.getLongitude();
        }
        return longitude;
    }

    // use this method to find if app can get the current location
    public boolean canGetMyLocation(){
        return this.canGetLocation;
    }

    public void showGPSDialog(){
        AlertDialog.Builder alert = new AlertDialog.Builder(myContext);
        // Setting Dialog Title 
        alert.setTitle("Location Setting");
        // Setting Dialog Message
        alert.setMessage("GPS is not enabled, do you want to enable this now in the settins menue?");

        // setting icon to dialog
        //alert.setIcon(R.drawable.)

        // on pressing settings button
        alert.setPositiveButton("Settins", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                myContext.startActivity(intent);
            }
        });
        //on pressing cancel button
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        // showing the alert dialog
        alert.show();
    }

    public void stopUsingGPS(){
        if(locManager !=null){
            locManager.removeUpdates(GPSHandling.this);
        }
    }


    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

3 个答案:

答案 0 :(得分:1)

您已经在代码中使用了LocationListener。你刚刚没有完全实现代码。

此方法在代码中为空:

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}

这是每次GPS产生位置更新时调用的方法。

您应该完成以下操作:

@Override
public void onLocationChanged(Location location) {
    latitude = location.getLatitude();
    longitude = location.getLongitude();
}

问候。

答案 1 :(得分:0)

您是否阅读了相应的文档? 首先看Location APILocationListener

LocationListener实际上非常简单易用,是唯一(干净)方式可靠地检索设备的位置。

答案 2 :(得分:0)

位置数据通常需要 30秒才能完全预热。当你要求getLastKnownLocation()时,确实可能会返回一个过时的位置。

Google自己在Location strategies了解详情。