我无法确定如何获取两个非固定位置之间的距离。第一个位置是我的当前位置,第二个位置是步行 100 米后的最新当前位置。下面是一段代码。
/**
* Callback that fires when the location changes.
*/
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
updateUI();
}
/**
* Updates the latitude, the longitude, and the last location time in the UI.
*/
private void updateUI() {
latitude = mCurrentLocation.getLatitude();
longitude = mCurrentLocation.getLongitude();
mLatitudeTextView.setText(String.format("%s: %f", mLatitudeLabel,latitude));
mLongitudeTextView.setText(String.format("%s: %f", mLongitudeLabel, longitude));
}
所以下面我有我的 mCurrentLocation 但如何获取 newCurrentLocation 所以我使用 distanceTo 方法。
double distance = mCurrentLocation.distanceTo(newCurrentLocation);
如有任何建议,我们将不胜感激。
最佳答案
您必须将当前位置保存在临时变量中,如下所示:
public void onLocationChanged(Location location) {
Location temp = mCurrentLocation; //save the old location
mCurrentLocation = location; //get the new location
distance = mCurrentLocation.distanceTo(temp); //find the distance
updateUI();
}
关于java - 如何获取当前位置和下一个当前位置之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35616139/