Android疯狂的速度和距离旅行



我已经创建了简单的程序来计算我的位置,并希望计算应用程序打开时用户的速度和总距离。该位置似乎运行良好,但是当我试图计算速度和距离时,它向我显示了一些疯狂的数字,而我的手机不移动,连接到计算机。那里怎么了?

这是我的代码:

public class MapsActivity extends AppCompatActivity {
LocationManager locationManager;
TextView tvSpeed;
TextView tvLong;
TextView tvLati;
TextView tvDistance;
double oldLat;
double oldLong;
double startTime;
double endTime;
double overalDistance;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    tvSpeed = (TextView) findViewById(R.id.textViewSpeed);
    tvLong = (TextView) findViewById(R.id.textViewLong);
    tvLati = (TextView) findViewById(R.id.textViewLati);
    tvDistance = (TextView) findViewById(R.id.textViewDistance);
    oldLat = 0;
    oldLong = 0;
    overalDistance = 0;
    startTime= SystemClock.elapsedRealtime();

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        return;
    }
    if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                double latitude = location.getLatitude();
                double longitude = location.getLongitude();
                endTime = SystemClock.elapsedRealtime();
                tvLati.setText("Latitude: " + Double.toString(latitude));
                tvLati.invalidate();
                tvLong.setText("Longitude: " + Double.toString(longitude));
                tvLong.invalidate();
                double distance = getDistance(oldLat,oldLong,latitude,longitude);
                double time = (endTime - startTime)/1000;
                startTime=endTime;
                oldLat = latitude;
                oldLong = longitude;
                tvSpeed.setText("Speed: " + Double.toString(distance/time));
                tvSpeed.invalidate();
                overalDistance += distance;
                tvDistance.setText("Overall Distance: " + Double.toString(overalDistance));
                tvDistance.invalidate();
            }
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
            @Override
            public void onProviderEnabled(String provider) {
            }
            @Override
            public void onProviderDisabled(String provider) {
            }
        });
    }
    else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                double latitude = location.getLatitude();
                double longitude = location.getLongitude();
                endTime = SystemClock.elapsedRealtime();
                tvLati.setText("Latitude: " + Double.toString(latitude));
                tvLati.invalidate();
                tvLong.setText("Longitude: " + Double.toString(longitude));
                tvLong.invalidate();
                double distance = getDistance(oldLat,oldLong,latitude,longitude);
                double time = (endTime - startTime)/1000;
                startTime=endTime;
                oldLat = latitude;
                oldLong = longitude;
                tvSpeed.setText("Speed: " + Double.toString(distance/time));
                tvSpeed.invalidate();
                overalDistance += distance;
                tvDistance.setText("Overall Distance: " + Double.toString(overalDistance));
                tvDistance.invalidate();
            }
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
            @Override
            public void onProviderEnabled(String provider) {
            }
            @Override
            public void onProviderDisabled(String provider) {
            }
        });
    }
}
public double getDistance(double lat1, double lon1, double lat2, double lon2)
{
    double latA = Math.toRadians(lat1);
    double lonA = Math.toRadians(lon1);
    double latB = Math.toRadians(lat2);
    double lonB = Math.toRadians(lon2);
    double cosAng = (Math.cos(latA) * Math.cos(latB) * Math.cos(lonB-lonA)) +
            (Math.sin(latA) * Math.sin(latB));
    double ang = Math.acos(cosAng);
    double dist = ang *6371;
    return dist;
}

这是我手机上的输出输出

您将 oldLatoldLong变量初始化为0。P>

您必须实现IF语句以不计算第一次距离:

@Override
public void onLocationChanged(Location location) {
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    tvLati.setText("Latitude: " + Double.toString(latitude));
    tvLati.invalidate();
    tvLong.setText("Longitude: " + Double.toString(longitude));
    tvLong.invalidate();
    if(oldLat == 0 && oldLong == 0) {
         oldLat = latitude;
         oldLong = longitude;
         return;
    }
    endTime = SystemClock.elapsedRealtime();
    double distance = getDistance(oldLat,oldLong,latitude,longitude);
    double time = (endTime - startTime)/1000;
    startTime=endTime;
    oldLat = latitude;
    oldLong = longitude;
    tvSpeed.setText("Speed: " + Double.toString(distance/time));
    tvSpeed.invalidate();
    overalDistance += distance;
    tvDistance.setText("Overall Distance: " + Double.toString(overalDistance));
    tvDistance.invalidate();
}

最新更新