0

現在のアプリケーションでは、返される位置データが古くなっています。更新間の最小時間/距離に基づいて更新を取得する方法を使用します。ただし、電話の電源を切り、別の都市まで車で移動してから、電話の電源を入れ直したとします。この場合、私の 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;
    }
}
4

3 に答える 3

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

よろしく。

于 2012-11-26T21:12:59.263 に答える
0

通常、位置データが完全にウォームアップするには最大 30 秒かかります。あなたが求めたようにgetLastKnownLocation()、確かに、古い場所が返される可能性があります.

詳細については、Google 自身によるロケーション戦略をご覧ください。

于 2012-11-26T17:30:40.703 に答える
0

対応するドキュメントを読みましたか?まず、 Location APILocationListenerを見てください。

LocationListener実際には非常に簡単に使用でき、デバイスの場所を確実に取得する唯一の (クリーンな) 方法です。

于 2012-11-26T17:28:22.937 に答える