0

我正在使用MapQuest Android API並繪製兩點之間的路線。我想要做的是在路線上找到多個點,例如沿路徑每隔100米指定一次緯度和長度值,並將其存儲在一個數組中。有沒有辦法做到這一點。我希望我的問題清楚明白。路線上的MapQuest點

回答

0

如果您嘗試減少路徑中的點數,但仍然保留精確的路徑表示,最好的方法是使用線簡化算法。

公共線,簡化算法是道格拉斯 - 普克,並有道格拉斯 - 普克的,你可以使用谷歌MyTracks項目的實施,在Apache許可V2.0: https://code.google.com/p/mytracks/source/browse/MyTracks/src/com/google/android/apps/mytracks/util/LocationUtils.java#78

下面的代碼,以防萬一鏈接斷裂:

/** 
* Decimates the given locations for a given zoom level. This uses a 
* Douglas-Peucker decimation algorithm. 
* 
* @param tolerance in meters 
* @param locations input 
* @param decimated output 
*/ 
private static void decimate(double tolerance, ArrayList<Location> locations, ArrayList<Location> decimated) { 
    final int n = locations.size(); 
    if (n < 1) { 
    return; 
    } 
    int idx; 
    int maxIdx = 0; 
    Stack<int[]> stack = new Stack<int[]>(); 
    double[] dists = new double[n]; 
    dists[0] = 1; 
    dists[n - 1] = 1; 
    double maxDist; 
    double dist = 0.0; 
    int[] current; 

    if (n > 2) { 
    int[] stackVal = new int[] { 0, (n - 1) }; 
    stack.push(stackVal); 
    while (stack.size() > 0) { 
     current = stack.pop(); 
     maxDist = 0; 
     for (idx = current[0] + 1; idx < current[1]; ++idx) { 
     dist = LocationUtils.distance(
      locations.get(idx), locations.get(current[0]), locations.get(current[1])); 
     if (dist > maxDist) { 
      maxDist = dist; 
      maxIdx = idx; 
     } 
     } 
     if (maxDist > tolerance) { 
     dists[maxIdx] = maxDist; 
     int[] stackValCurMax = { current[0], maxIdx }; 
     stack.push(stackValCurMax); 
     int[] stackValMaxCur = { maxIdx, current[1] }; 
     stack.push(stackValMaxCur); 
     } 
    } 
    } 

    int i = 0; 
    idx = 0; 
    decimated.clear(); 
    for (Location l : locations) { 
    if (dists[idx] != 0) { 
     decimated.add(l); 
     i++; 
    } 
    idx++; 
    } 
    Log.d(Constants.TAG, "Decimating " + n + " points to " + i + " w/ tolerance = " + tolerance); 
} 
0

我在Java和Actionscript中做了類似的事情。首先,如果你還沒有意識到,你所擁有的路線中的每一條腿都是直線。路線越平直,你的腿就越少。

要確定沿着路線等距離的點 - 或者在我的情況下沿着路線的行駛時間 - 只需循環計算每條腿末端的累積距離[lat,long]座標集。

使用此信息,您可以輕鬆修改您的循環以檢查沿着腿部發生距離點(或多個點)的位置。記住每條腿是一條直線。如果是這樣,它足夠簡單,可以識別沿着你正在尋找的腿部的距離,並從中計算遠處的腿部。

爲此需要2種算法:

  1. 之間的距離2 [緯度,經度]座標
  2. 即距離x沿着之間2 [緯度,經度]座標
一行中的點

說起來容易做起來難,這些都是複雜的算法,網上有一些可疑的例子。在我看來,很少有人在解釋數學如何運作方面做得很好,而且我發現的mot是不完整的。

然後我發現這個寶石:http://www.movable-type.co.uk/scripts/latlong.html

我實現我的算法在ActionScript和Java基於由克里斯俯伏這個很好的參考。

鏈接應該提供您需要的一切。對你需要做什麼的解釋,psudocode和javascript中的簡明算法,以及如果這還不夠,可以用來測試你的算法。