textview中的android gps距离显示应用程序第二次运行时的上一个距离

时间:2013-12-18 05:08:41

标签: android gps

我有一个应用程序,它计算每100米后的距离。有一个按钮和一个文本视图。当我单击按钮并开始行走时,距离显示在文本视图中。当我第二次点击按钮到达目的地后,应用程序停止。但是当我第二次启动应用程序时,它会在文本视图中显示前一个距离,但它应该为0。

这是我的活动:

public class Gps extends Activity {

TextView display;
Button start;
boolean doubleclick = false;
AnimationDrawable AppNameAnimation;
private boolean enabled;
double currentLon = 0;
double currentLat = 0;
double lastLon = 0;
double lastLat = 0;
double distance;
Animation animRotate;
TextView startStop;

LocationManager lm;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_test);
    startStop = (TextView) findViewById(R.id.textView2);
    display = (TextView) findViewById(R.id.textView1);
    start = (Button) findViewById(R.id.button1);
    animRotate = AnimationUtils.loadAnimation(this,
            R.anim.anim_rotate);

    start.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (!doubleclick) {
                lm = (LocationManager) getSystemService(LOCATION_SERVICE);
                enabled = lm
                        .isProviderEnabled(LocationManager.GPS_PROVIDER);
                if (!enabled) {
                    Intent inte = new Intent(
                            Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(inte);
                } else {
                    lm.requestLocationUpdates(lm.GPS_PROVIDER, 0, 0,
                            Loclist);
                    Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);

                    if (loc == null) {
                        display.setText("No GPS location found");
                    } else {
                        // set Current latitude and longitude
                        currentLon = loc.getLongitude();
                        currentLat = loc.getLatitude();
                        Log.e("Location", "currentLat:" + currentLat);

                    }
                    // Set the last latitude and longitude
                    startStop.setText("Stop");
                    lastLat = currentLat;
                    lastLon = currentLon;
                    doubleclick = true;
                }
            } else {
                lm.removeUpdates(Loclist);
                startStop.setText("Start");
                v.clearAnimation();
                doubleclick = false;
                Intent in = new Intent(Gps.this, NextActivity.class);
                //in.putExtra("distance", display.toString());
                startActivity(in);
                display.setText("0.0km");
            }

        }
    });
}

LocationListener Loclist = new LocationListener() {

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

        // start location manager
        LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);

        // Get last location
        Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);

        // Request new location
        lm.requestLocationUpdates(lm.GPS_PROVIDER, 0, 0, Loclist);

        // Get new location
        Location loc2 = lm.getLastKnownLocation(lm.GPS_PROVIDER);

        // get the current lat and long
        currentLat = loc.getLatitude();
        currentLon = loc.getLongitude();


        Location locationA = new Location("point A");
        locationA.setLatitude(lastLat);
        locationA.setLongitude(lastLon);

        Location locationB = new Location("point B");
        locationB.setLatitude(currentLat);
        locationB.setLongitude(currentLon);
        if (lastLat != 0 || lastLon != 0) {
            double distanceMeters = locationA.distanceTo(locationB);

            double distanceKm = distanceMeters / 1000f;

            display.setText(String.format("%.2f Km", distanceKm));
            Constant.distance = (String.format("%.2f Km", distanceKm));
            start.startAnimation(animRotate);
        }

    }

    @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
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    display.setText("0.0km");

}



}

请帮帮我。

2 个答案:

答案 0 :(得分:0)

start.setOnClickListener(new View.OnClickListener() {});

之前添加这些行
 display.setText("0.0km");

<强>编辑:

在locationChanged listener中更改此行。就像这样:

 // get the current lat and long
    currentLat = loc2.getLatitude();
    currentLon = loc2.getLongitude();

全班将是这样的:

public class Gps extends Activity {    
TextView display;
Button start;
boolean doubleclick = false;
AnimationDrawable AppNameAnimation;
private boolean enabled;
double currentLon = 0;
double currentLat = 0;
double lastLon = 0;
double lastLat = 0;
double distance;
Animation animRotate;
TextView startStop;

LocationManager lm;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_test);
    startStop = (TextView) findViewById(R.id.textView2);
    display = (TextView) findViewById(R.id.textView1);
    start = (Button) findViewById(R.id.button1);
    animRotate = AnimationUtils.loadAnimation(this,
            R.anim.anim_rotate);

lm = (LocationManager) getSystemService(LOCATION_SERVICE); 
lm.removeUpdates(Loclist); 
display.setText("0.0km");

    start.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (!doubleclick) {
                lm = (LocationManager) getSystemService(LOCATION_SERVICE);
                enabled = lm
                        .isProviderEnabled(LocationManager.GPS_PROVIDER);
                if (!enabled) {
                    Intent inte = new Intent(
                            Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(inte);
                } else {
                    lm.requestLocationUpdates(lm.GPS_PROVIDER, 0, 0,
                            Loclist);
                    Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);

                    if (loc == null) {
                        display.setText("No GPS location found");
                    } else {
                        // set Current latitude and longitude
                        currentLon = loc.getLongitude();
                        currentLat = loc.getLatitude();
                        Log.e("Location", "currentLat:" + currentLat);

                    }
                    // Set the last latitude and longitude
                    startStop.setText("Stop");
                    lastLat = currentLat;
                    lastLon = currentLon;
                    doubleclick = true;
                }
            } else {
                lm.removeUpdates(Loclist);
                startStop.setText("Start");
                v.clearAnimation();
                doubleclick = false;
                Intent in = new Intent(Gps.this, NextActivity.class);
                //in.putExtra("distance", display.toString());
                startActivity(in);
                display.setText("0.0km");
            }

        }
    });
}

LocationListener Loclist = new LocationListener() {

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

        // start location manager
        LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);

        // Get last location
        Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);

        // Request new location
        lm.requestLocationUpdates(lm.GPS_PROVIDER, 0, 0, Loclist);

        // Get new location
        Location loc2 = lm.getLastKnownLocation(lm.GPS_PROVIDER);

        // get the current lat and long
        currentLat = loc.getLatitude();
        currentLon = loc.getLongitude();


        Location locationA = new Location("point A");
        locationA.setLatitude(lastLat);
        locationA.setLongitude(lastLon);

        Location locationB = new Location("point B");
        locationB.setLatitude(currentLat);
        locationB.setLongitude(currentLon);
        if (lastLat != 0 || lastLon != 0) {
            double distanceMeters = locationA.distanceTo(locationB);

            double distanceKm = distanceMeters / 1000f;

            display.setText(String.format("%.2f Km", distanceKm));
            Constant.distance = (String.format("%.2f Km", distanceKm));
            start.startAnimation(animRotate);
        }

    }

    @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
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    display.setText("0.0km");

}



}

答案 1 :(得分:0)

设置:

   display.setText("0.0km");

if(!doubleclick)条件下。

您也可以在onLocationChanged的参数中使用location对象,如下所示:

  @Override
    public void onLocationChanged(Location location) {


        // get the current lat and long
        currentLat = location.getLatitude();
        currentLon = location.getLongitude();


        Location locationA = new Location("point A");
        locationA.setLatitude(lastLat);
        locationA.setLongitude(lastLon);

        Location locationB = new Location("point B");
        locationB.setLatitude(currentLat);
        locationB.setLongitude(currentLon);
        if (lastLat != 0 || lastLon != 0) {
            double distanceMeters = locationA.distanceTo(locationB);

            double distanceKm = distanceMeters / 1000f;

            display.setText(String.format("%.2f Km", distanceKm));
            Constant.distance = (String.format("%.2f Km", distanceKm));
            start.startAnimation(animRotate);
        }

    }

编辑:

 LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);

获取位置管理员的参考。

 Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);

位置管理员会使用此方法跟踪您可以直接获得的最后已知位置。

 lm.requestLocationUpdates(lm.GPS_PROVIDER, 0, 0, Loclist);

这就像每次调用位置更改onLocationChange时都会添加一个位置更改的侦听器。 现在你已经完成了点击按钮已经没有必要再做了。您的侦听器已附加,并将多次调用onLocationChange。