9

안드로이드에서 Google Maps API V2로 놀고있었습니다. JSON 구문 분석을 사용하여 두 위치간에 경로를 가져 오려고합니다.Google지도 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(); 
    } 
} 

내 경로를 그릴 그리고 데코 사전에 AsyncTask를

감사로 코딩 한 후

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; 
} 

그리고 내 JSON 드.

+0

감사합니다 ... 제대로 렌더링

감사하지 않았다. :) – rptwsthi

답변

2

오래 기다려서 죄송합니다. 잠시 전에 고칠 수 있었지만 아직 내 해결책을 제시하지 못했습니다.

그것은 두 번째 할 일에

while (b >= 0x20); 

에 문을 내가 "="잊었 문 동안 동안 내 JSON 디코더에서

나는이 할 일을 사용하여 ... 기본적으로 오타했다. 따라서 그것은 도움이 코드를 공유

1

LatLng 개체를 overview_polyline에서 생성한다고 생각합니다. Google 문서 에 따르면 "결과 방향의 대략적인 (평탄한) 경로를 나타내는 인코딩 된 점의 배열을 보유하고있는 객체가 있습니다.".

나는 당신이 공식 문서가 단계는 단일 단계를 포함하는 방향 경로의 최소 단위입니다 상태로 legs[]steps[] 데이터를 기반으로 LatLng 객체를 구축 자세한 경로를 얻을 수 있다는 확신 여행에 대한 특정 단일 명령을 설명합니다..

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

0

Tmichel, 마이클 거리 밖으로 제대로 때문에 경로 음모에 다리와 단계에 웨이브 라인이 있습니다

는에서 살펴 보자. 다리와 계단에는 좌표를 중심으로 사용자에게 경고하는 정보가 있습니다.

폴리 라인은 길 위에서 정확하고 정확한 점입니다. 죄송합니다, 내 영어가