我对Java& Android和我在代码方面存在一些问题。
location1.distanceTo(location2)
方法是否也计算考虑高程变化的距离?
另一个问题是,虽然我没有移动设备,但测量的距离会发生变化。 这是我的代码:
public class GpsCalculator{
public Activity activity;
private LocationManager locationManager = null;
private Location previousLocation = null;
private double totalDistance = 0D;
private Context context;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; // 1 meter
private static final long MIN_TIME_BW_UPDATES = 1000; // 1 second
TextView distance_tracker;
public GpsCalculator(TextView dt) {
distance_tracker = dt;
}
public void run(Context act){
context = act;
// Get the location manager
locationManager = (LocationManager) context.getSystemService(Service.LOCATION_SERVICE);
// Add new listeners with the given params
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener); // Network location
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener); // Gps location
}
public void stop(){
locationManager.removeUpdates(locationListener);
}
DecimalFormat df = new DecimalFormat("#");
private LocationListener locationListener = new LocationListener(){
@Override
public void onLocationChanged(Location newLocation){
if (previousLocation != null){
GpsCalculator.this.totalDistance += previousLocation.distanceTo(newLocation);
}
// Update stored location
GpsCalculator.this.previousLocation = newLocation;
CharSequence text = Double.toString(totalDistance);
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
distance_tracker.setText(df.format(totalDistance)+ " m");
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}; }
我想提一下,无论是否有网络提供商监听,准确性问题都会持续存在。
结论
代码是正确的,但GPS数据的准确性并非如此。我从Carsten的答案中的评论中理解了这一点。
答案 0 :(得分:1)
位置对象通常还包含高度信息,因此会考虑高程。
更新之间的位置是否有变化?我想是这样的,因为它会在你的情况下每秒更新一次。