距离计算错误 android
Distance calculation bug android
您好,我一直在做计算行驶距离和速度的应用程序。获得速度和距离的所有功能都可以正常工作,但是在测试过程中我看到只有当速度为 19 Km/h +
时才开始测量距离的错误
有人知道为什么会这样吗?
谢谢
//initialize location listener
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
getSpeed(location);
getDistance(location);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
//get the speed from the given location updates
public void getSpeed(Location location) {
currentSpeed = (location.getSpeed() * 3600 / 1000);
String convertedSpeed = String.format("%.2f", currentSpeed);
speedTxt.setText(convertedSpeed + "Km/h");
}
private void getDistance(Location location) {
//to capture current location and keep as starting position of person
if (pLat == 500.0 && pLng == 500.0 ){
pLat = location.getLatitude();
pLng = location.getLongitude();
}
if (cLat == 500.0 && cLng == 500.0){
cLat = location.getLatitude();
cLng=location.getLongitude();
}
//to check is the person has changed location
if (pLat != cLat && pLng != cLng) {
pLat = cLat;
pLng = cLng;
}
//update the current location
cLat = location.getLatitude();
cLng = location.getLongitude();
//call the calculation method
distance += getDistanceBetweenGeoPoints(cLat, cLng, pLat, pLng);
String convertedDistance = String.format("%.2f", distance);
distanceTxt.setText(" " + convertedDistance);
}
public double getDistanceBetweenGeoPoints(Double cLat, Double cLng, Double pLat, Double pLng) {
// CALCULATE DISTANCE BETWEEN TWO POINTS
double earthRadius = 6367; //meters
double dLat = Math.toRadians(cLat - pLat);
double dLng = Math.toRadians(cLng - pLng);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(Math.toRadians(cLat)) * Math.cos(Math.toRadians(pLat)) *
Math.sin(dLng / 2) * Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double dist = (double) (earthRadius * c);
dist = Math.round(dist * 100) / 100.0;
return dist;
}
};
由于这条线,您的 getDistanceBetweenGeoPoints 函数将 return 由于位置上的微小差异正好为 0:
dist = Math.round(dist * 100) / 100.0;
为了在 GetDistance() 中显示,尝试四舍五入总距离:
//call the calculation method
distance += getDistanceBetweenGeoPoints(cLat, cLng, pLat, pLng);
String convertedDistance = String.format("%.2f", Math.round(dist * 100) / 100.0);
distanceTxt.setText(" " + convertedDistance);
private void getDistance(Location location) {
//to capture current location and keep as starting position of person
if (pLat == 500.0 && pLng == 500.0) {
pLat = location.getLatitude();
pLng = location.getLongitude();
}
if (cLat == 500.0 && cLng == 500.0) {
cLat = location.getLatitude();
cLng = location.getLongitude();
}
//to check is the person has changed location
if (pLat != cLat && pLng != cLng) {
pLat = cLat;
pLng = cLng;
}
//update the current location
cLat = location.getLatitude();
cLng = location.getLongitude();
//call the calculation method
distance += getDistanceBetweenGeoPoints(cLat, cLng, pLat, pLng);
//String convertedDistance = String.format("%.2f", distance);
String convertedDistance = String.format("%.2f", Math.round(distance * 100) / 100.0);
distanceTxt.setText(" " + convertedDistance);
}
public double getDistanceBetweenGeoPoints(Double cLat, Double cLng, Double pLat, Double pLng) {
// CALCULATE DISTANCE BETWEEN TWO POINTS
double earthRadius = 6367; //meters
double dLat = Math.toRadians(cLat - pLat);
double dLng = Math.toRadians(cLng - pLng);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(Math.toRadians(cLat)) * Math.cos(Math.toRadians(pLat)) *
Math.sin(dLng / 2) * Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double dist = (double) (earthRadius * c);
//dist = Math.round(dist * 100) / 100.0;
return dist;
}
您好,我一直在做计算行驶距离和速度的应用程序。获得速度和距离的所有功能都可以正常工作,但是在测试过程中我看到只有当速度为 19 Km/h +
时才开始测量距离的错误有人知道为什么会这样吗?
谢谢
//initialize location listener
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
getSpeed(location);
getDistance(location);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
//get the speed from the given location updates
public void getSpeed(Location location) {
currentSpeed = (location.getSpeed() * 3600 / 1000);
String convertedSpeed = String.format("%.2f", currentSpeed);
speedTxt.setText(convertedSpeed + "Km/h");
}
private void getDistance(Location location) {
//to capture current location and keep as starting position of person
if (pLat == 500.0 && pLng == 500.0 ){
pLat = location.getLatitude();
pLng = location.getLongitude();
}
if (cLat == 500.0 && cLng == 500.0){
cLat = location.getLatitude();
cLng=location.getLongitude();
}
//to check is the person has changed location
if (pLat != cLat && pLng != cLng) {
pLat = cLat;
pLng = cLng;
}
//update the current location
cLat = location.getLatitude();
cLng = location.getLongitude();
//call the calculation method
distance += getDistanceBetweenGeoPoints(cLat, cLng, pLat, pLng);
String convertedDistance = String.format("%.2f", distance);
distanceTxt.setText(" " + convertedDistance);
}
public double getDistanceBetweenGeoPoints(Double cLat, Double cLng, Double pLat, Double pLng) {
// CALCULATE DISTANCE BETWEEN TWO POINTS
double earthRadius = 6367; //meters
double dLat = Math.toRadians(cLat - pLat);
double dLng = Math.toRadians(cLng - pLng);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(Math.toRadians(cLat)) * Math.cos(Math.toRadians(pLat)) *
Math.sin(dLng / 2) * Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double dist = (double) (earthRadius * c);
dist = Math.round(dist * 100) / 100.0;
return dist;
}
};
由于这条线,您的 getDistanceBetweenGeoPoints 函数将 return 由于位置上的微小差异正好为 0:
dist = Math.round(dist * 100) / 100.0;
为了在 GetDistance() 中显示,尝试四舍五入总距离:
//call the calculation method
distance += getDistanceBetweenGeoPoints(cLat, cLng, pLat, pLng);
String convertedDistance = String.format("%.2f", Math.round(dist * 100) / 100.0);
distanceTxt.setText(" " + convertedDistance);
private void getDistance(Location location) {
//to capture current location and keep as starting position of person
if (pLat == 500.0 && pLng == 500.0) {
pLat = location.getLatitude();
pLng = location.getLongitude();
}
if (cLat == 500.0 && cLng == 500.0) {
cLat = location.getLatitude();
cLng = location.getLongitude();
}
//to check is the person has changed location
if (pLat != cLat && pLng != cLng) {
pLat = cLat;
pLng = cLng;
}
//update the current location
cLat = location.getLatitude();
cLng = location.getLongitude();
//call the calculation method
distance += getDistanceBetweenGeoPoints(cLat, cLng, pLat, pLng);
//String convertedDistance = String.format("%.2f", distance);
String convertedDistance = String.format("%.2f", Math.round(distance * 100) / 100.0);
distanceTxt.setText(" " + convertedDistance);
}
public double getDistanceBetweenGeoPoints(Double cLat, Double cLng, Double pLat, Double pLng) {
// CALCULATE DISTANCE BETWEEN TWO POINTS
double earthRadius = 6367; //meters
double dLat = Math.toRadians(cLat - pLat);
double dLng = Math.toRadians(cLng - pLng);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(Math.toRadians(cLat)) * Math.cos(Math.toRadians(pLat)) *
Math.sin(dLng / 2) * Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double dist = (double) (earthRadius * c);
//dist = Math.round(dist * 100) / 100.0;
return dist;
}