java - 如何获取当前位置和下一个当前位置之间的距离

标签 java android gps geolocation android-location

我无法确定如何获取两个非固定位置之间的距离。第一个位置是我的当前位置,第二个位置是步行 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/

相关文章:

Java,返回字符串错误

java - 如何使用从 dll 在 Java 端声明的 log4j 记录器

javascript - 在 Cordova 地理定位中访问模拟蓝牙 GPS 坐标

java - 将基坐标与 n 坐标列表进行比较并确定最接近的 m 坐标的最佳算法?

java - 并发多线程批量向Mysql插入/更新数据

java - 标记类的子类

android - 光标下划线

android - 在android中获取联系人非常慢

android - 遍历 SQLite Cursor 需要太多时间

ios - 将大量 GPS 数据异步转换为城市/国家