簡體   English   中英

如何獲得當前位置和下一個當前位置之間的距離

[英]How to get distance between current location and the next current location

我在弄清楚如何獲取2個非固定位置之間的距離時遇到了麻煩。 第一個位置是我的當前位置,第二個位置是走了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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM