2014-03-07 26 views
2

之間畫路徑是什麼我想這樣做是:的Android:谷歌地圖 - 多點

我收到的方向/路徑列表(即用戶將不得不使用我的應用程序跟蹤)。 我在繪製地圖上的路徑時遇到了問題。方向/路徑包含街道名稱,街道座標和街道部分。

我無法弄清楚如何在地圖上繪製路徑/路線並更新路線 - 例如,當用戶移動(在路上)移動圖標時,指示用戶的進度或繪製的線條路線變短了,這真的沒什麼關係。那麼你能指點我可以參考的教程嗎?

我已經看到了很多,到目前爲止,但其中大部分得到谷歌地圖的指示或繪製的線是從起點只是直線結束點,並在所有不適合在街上。

回答

1

試試下面這個教程。您應該在用戶位置和標記之間繪製。在用戶端調用函數onLocationChange獲取實際位置並重畫線條。 http://wptrafficanalyzer.in/blog/driving-route-from-my-location-to-destination-in-google-maps-android-api-v2/

+0

嗯,我認爲你不明白的問題。我不想使用Google地圖來繪製地圖我想要通過給定的點來繪製路線。 – user3182266

+1

這裏是一個如何在地圖上繪製路線的例子。抽獎部分是最有用答案的最後部分。 http://stackoverflow.com/questions/14444228/android-how-to-draw-route-directions-google-maps-api-v2-from-current-location-t – bogdand

+0

upvote for desire to help – ismail

0

爲此,請按照下列步驟的ArrayList markerPoints的

  1. 獲取列表;

  2. 單一路徑創建標記,

    LatLng origin = markerPoints.get(0); 
    LatLng dest = markerPoints.get(1); 
    
    // Getting URL to the Google Directions API 
    String url = getDirectionsUrl(origin, dest); 
    
    DownloadTask downloadTask = new DownloadTask(); 
    
    // Start downloading json data from Google Directions API 
    downloadTask.execute(url); 
    
  3. 多個目的地的路徑,例如ABDC等

    private List<String> getDirectionsUrl(ArrayList<LatLng> markerPoints) { 
    List<String> mUrls = new ArrayList<>(); 
    if (markerPoints.size() > 1) { 
        String str_origin = markerPoints.get(0).latitude + "," + markerPoints.get(0).longitude; 
        String str_dest = markerPoints.get(1).latitude + "," + markerPoints.get(1).longitude; 
    
        String sensor = "sensor=false"; 
        String parameters = "origin=" + str_origin + "&destination=" + str_dest + "&" + sensor; 
        String output = "json"; 
        String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters; 
    
        mUrls.add(url); 
        for (int i = 2; i < markerPoints.size(); i++)//loop starts from 2 because 0 and 1 are already printed 
        { 
         str_origin = str_dest; 
         str_dest = markerPoints.get(i).latitude + "," + markerPoints.get(i).longitude; 
         parameters = "origin=" + str_origin + "&destination=" + str_dest + "&" + sensor; 
         url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters; 
         mUrls.add(url); 
        } 
    } 
    
    return mUrls; 
    } 
    
  4. 呼叫從

    上述方法
    List<String> urls = getDirectionsUrl(markerPoints); 
        if (urls.size() > 1) { 
          for (int i = 0; i < urls.size(); i++) { 
          String url = urls.get(i); 
          DownloadTask downloadTask = new DownloadTask(); 
          // Start downloading json data from Google Directions API 
            downloadTask.execute(url); 
           } 
          } 
        } 
    

    上面的代碼將呼籲建立多條路徑,如A-B,B-d,d-C等