Android疯狂的速度和距离旅行

时间:2017-04-21 11:56:35

标签: android location distance

我已经创建了用于计算我的位置的简单程序,并且想要计算应用程序启动时用户所行进的速度和总体距离。 位置似乎运行良好,但当我试图计算速度和距离时,它显示了一些疯狂的数字,而我的手机没有移动,连接到计算机。那有什么不对?

这是我的代码:

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

这是我手机上的输出 Output

1 个答案:

答案 0 :(得分:0)

您将oldLatoldLong变量初始化为0.因此,当您的应用首次使用某个位置时,它会计算您当前位置与(0,0)坐标之间的距离。

你必须实现一个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();
}