我想实时获取距离,我在互联网上发现了很多关于它的帖子,但到目前为止我还没能使它工作,我不确定哪个是最好的选择。我还想在TextView中显示距离。 而且我想知道我是否必须使用“distanceTo”或“distance between”
到目前为止,这就是我所拥有的,我跟着这个,但我感到困惑! 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) {
}
};
答案 0 :(得分:1)
每次新位置进入时,只需增加行进距离。
使用成员变量存储以前的位置,到目前为止的行进距离以及对要更新的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) {}
};