9

我在Android上玩Google Maps API V2。 嘗試獲取2個位置之間的路徑,並使用JSON解析進行此操作。繪製2個位置之間的路線Google Maps API Android V2

我正在找路線。路線開始如何應該。但在某一時刻它走錯了路。

我的最終目的地結束了錯誤。和其他一些地點,我的應用程序只是被終止。

這是我做了什麼

這裏是我的makeURL方法

public String makeUrl(){ 
    StringBuilder urlString = new StringBuilder(); 
    urlString.append("http://maps.googleapis.com/maps/api/directions/json"); 
    urlString.append("?origin="); //start positie 
    urlString.append(Double.toString(source.latitude)); 
    urlString.append(","); 
    urlString.append(Double.toString(source.longitude)); 
    urlString.append("&destination="); //eind positie 
    urlString.append(Double.toString(dest.latitude)); 
    urlString.append(","); 
    urlString.append(Double.toString(dest.longitude)); 
    urlString.append("&sensor=false&mode=driving"); 

    return urlString.toString(); 
} 

我的JSON解析器

public class JSONParser { 

static InputStream is = null; 
static JSONObject jObj = null; 
static String json = ""; 

public JSONParser() { 
    // TODO Auto-generated constructor stub 
} 

public String getJSONFromURL(String url){ 

    try { 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 

     is = httpEntity.getContent(); 
    } catch(UnsupportedEncodingException e){ 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 

     while((line = reader.readLine()) != null){ 
      sb.append(line + "\n"); 
      //Log.e("test: ", sb.toString()); 
     } 

     json = sb.toString(); 
     is.close(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     Log.e("buffer error", "Error converting result " + e.toString()); 
    } 

    return json; 
} 

我畫我的道路用這種方法

public void drawPath(String result){ 
    try{ 
     final JSONObject json = new JSONObject(result); 
     JSONArray routeArray = json.getJSONArray("routes"); 
     JSONObject routes = routeArray.getJSONObject(0); 

     JSONObject overviewPolylines = routes.getJSONObject("overview_polyline"); 
     String encodedString = overviewPolylines.getString("points"); 
     Log.d("test: ", encodedString); 
     List<LatLng> list = decodePoly(encodedString); 

     LatLng last = null; 
     for (int i = 0; i < list.size()-1; i++) { 
      LatLng src = list.get(i); 
      LatLng dest = list.get(i+1); 
      last = dest; 
      Log.d("Last latLng:", last.latitude + ", " + last.longitude); 
      Polyline line = googleMap.addPolyline(new PolylineOptions().add( 
        new LatLng(src.latitude, src.longitude), new LatLng(dest.latitude, dest.longitude)) 
        .width(2) 
        .color(Color.BLUE)); 
     } 

     Log.d("Last latLng:", last.latitude + ", " + last.longitude); 
    }catch (JSONException e){ 
     e.printStackTrace(); 
    } 
} 

我裝飾去我的JSON與

private List<LatLng> decodePoly(String encoded){ 

    List<LatLng> poly = new ArrayList<LatLng>(); 
    int index = 0; 
    int length = encoded.length(); 
    int latitude = 0; 
    int longitude = 0; 

    while(index < length){ 
     int b; 
     int shift = 0; 
     int result = 0; 

     do { 
      b = encoded.charAt(index++) - 63; 
      result |= (b & 0x1f) << shift; 
      shift += 5; 
     } while (b >= 0x20); 

     int destLat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 
     latitude += destLat; 

     shift = 0; 
     result = 0; 
     do { 
      b = encoded.charAt(index++) - 63; 
      result |= (b & 0x1f) << shift; 
      shift += 5; 
     } while (b > 0x20); 

     int destLong = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 
     longitude += destLong; 

     poly.add(new LatLng((latitude/1E5),(longitude/1E5))); 
    } 
    return poly; 
} 

,然後用事先的的AsyncTask

感謝編碼。

+0

感謝您分享它幫助的代碼。 :) – rptwsthi 2013-09-09 08:08:54

回答

2

對不起,等了很久..我已經修復了一段時間,但我還沒有把我的解決方案放在這裏呢。

它基本上是一個錯字...

在我的Json解碼器我用2個do while與

while (b >= 0x20); 

語句在第二個do while語句我忘了 「=」。 因此它沒有正確渲染...

謝謝

1

我相信你是從overview_polyline創建你的LatLng對象。根據谷歌文檔「這包含一個對象,該對象持有代表所得方向的近似(平滑)路徑的編碼點陣列。」

我敢肯定,你可以得到一個更詳細的路線構建基於legs[]steps[]數據的LatLng對象作爲官方文件指出步驟是導航路線中最小的組成單位,其中包含一個單一的步驟描述旅程中的特定單條指令

https://developers.google.com/maps/documentation/directions/#Routes

0

Tmichel, 邁克爾有正確的波,因爲腿和路線情節的步驟行出街:

在看看。 腿和步驟,有關於座標信息提醒用戶的信息。

多義線是街道上正確和精確的點。 對不起我的英文不好

相關問題