Android:如何计算使用GPS旅行的距离

时间:2013-12-10 05:38:54

标签: android gps

我有一个应用程序,它计算每100米后的距离。有一个按钮和一个文本视图。当我单击按钮时,每隔100米后计算出的距离并在文本视图中显示。但我发现了一个错误。当我第一次吃午餐时,文本视图应该是0.0km,但最初它显示75000km。我不知道为什么。请帮我。这对我来说非常重要。

我还有一个问题,我使用的按钮是一个动画按钮。当我单击Button时,按钮旋转(Animation Start v.startAnimation(animRotate))。获取位置时我需要动画。我必须把动画放在哪里(v.startAnimation(animRotate))。

这是我的活动:

public class Gps extends Activity {

TextView display;
Button start;
boolean doubleclick = false;
AnimationDrawable AppNameAnimation;

double currentLon = 0;
double currentLat = 0;
double lastLon = 0;
double lastLat = 0;
double distance;
TextView startStop;

LocationManager lm;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    startStop = (TextView) findViewById(R.id.textView2);
    display = (TextView) findViewById(R.id.textView1);
    start = (Button) findViewById(R.id.button1);
    final Animation 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) {
                v.startAnimation(animRotate);
                startStop.setText("Stop");
                lm = (LocationManager) getSystemService(LOCATION_SERVICE);
                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();

                }
                // Set the last latitude and longitude
                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);
                startActivity(in);
            }

        }
    });
}

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

        double distanceMeters = locationA.distanceTo(locationB);

        double distanceKm = distanceMeters / 1000f;

        display.setText(String.format("%.2f Km", distanceKm));

    }

    @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

    }

};

0 个答案:

没有答案