我想要实时获得距离,我已经在互联网上找到了很多关于它的帖子,但到目前为止我还不能让它工作,我不确定哪一个是最好的选择。我还想在TextView中显示距离。我想知道我是必须用"distanceTo“还是"distancebetween”
到目前为止,这就是我所拥有的,我一直在关注这个,但我感到困惑!How to use android distanceBetween() method
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
Location oldLoc=0, newLoc=0;
location.getLatitude();
location.getLongitude();
if(oldLoc!=null){
if(newLoc == null){
newLoc = location;
}
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};发布于 2018-03-31 08:47:59
只需在每次新位置到来时增加行程即可。
使用成员变量存储以前的位置、到目前为止行驶的距离以及对要更新的TextView的引用:
Location mPrevLocation = null;
float mDistanceTravelled = 0.0F;
TextView mDistanceTextView;然后,使用以下代码来跟踪距离,并更新TextView:
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (mPrevLocation != null) {
mDistanceTravelled += mPrevLocation.distanceTo(location);
mDistanceTextView.setText(String.valueOf(mDistanceTravelled));
}
mPrevLocation = location;
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
};https://stackoverflow.com/questions/49583033
复制相似问题