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, 迈克尔有正确的波,因为腿和路线情节的步骤行出街:

在看看。 腿和步骤,有关于坐标信息提醒用户的信息。

多义线是街道上正确和精确的点。 对不起我的英文不好

相关问题