2017-09-21 135 views
0

我想根據谷歌方向的結果繪製方向的方向Api,從這個URL我想解析響應並在谷歌地圖上繪製路線。繪製方向和解析方向api overview_polyline

谷歌方向API網址 https://maps.googleapis.com/maps/api/directions/json?origin=10.1849092,76.37530459999999&destination=10.308027199999998,76.3336779&sensor=false&waypoints=10.2269749,76.3750218|10.263201599999999,76.3492569|10.283437099999997,76.3420206|

從我所採取的"routes"值的結果 - >"overview_polyline" - >"points"到DEAW行,我有 「點」 值

​​

現在我想解析這個並繪製路線圖。

+0

轉寄此它會幫助你https://www.journaldev.com/13373/android-google-map-drawing-route-two-points – RajatN

回答

0

我使用drawDirectionToStop()繪製的路線,我傳遞到「點」 decodeOverviewPolyLinePonts()的解析值,

private void drawDirectionToStop(DirectionData.Overview_polyline overviewPolyline) { 
    if (overviewPolyline != null) { 
     List<LatLng> polyz = decodeOverviewPolyLinePonts(overviewPolyline.getPoints()); 
     if (polyz != null) { 
      PolylineOptions lineOptions = new PolylineOptions(); 
      lineOptions.addAll(polyz); 
      lineOptions.width(5); 
      lineOptions.color(ContextCompat.getColor(getActivity(), R.color.colorAccent)); 
      mGoogleMap.addPolyline(lineOptions); 
     } 
    } 
} 


//This function is to parse the value of "points" 
public List<LatLng> decodeOverviewPolyLinePonts(String encoded) { 
    List<LatLng> poly = new ArrayList<LatLng>(); 
    if (encoded != null && !encoded.isEmpty() && encoded.trim().length() > 0) { 
     int index = 0, len = encoded.length(); 
     int lat = 0, lng = 0; 

     while (index < len) { 
      int b, shift = 0, result = 0; 
      do { 
       b = encoded.charAt(index++) - 63; 
       result |= (b & 0x1f) << shift; 
       shift += 5; 
      } while (b >= 0x20); 
      int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 
      lat += dlat; 

      shift = 0; 
      result = 0; 
      do { 
       b = encoded.charAt(index++) - 63; 
       result |= (b & 0x1f) << shift; 
       shift += 5; 
      } while (b >= 0x20); 
      int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 
      lng += dlng; 

      LatLng p = new LatLng((((double) lat/1E5)), 
        (((double) lng/1E5))); 
      poly.add(p); 
     } 
    } 
    return poly; 
} 
0

你可以使用這個庫來簡單地畫上Google Maps折線:

編譯 'com.cs:googlemaproute:1.0.0'

然後實現接口:

實現DrawRoute.onDrawRoute

現在只需畫出折線像這樣:

DrawRoute.getInstance(this,RouteActivity.this).setFromLatLong(24.905954,67.0803505) 
      .setToLatLong(24.9053485,67.079119).setGmapAndKey("MapandroidKey",gMap).run(); 

回調監聽

此偵聽器返回的狀態消息和位置,從谷歌獲得。

@Override 
    public void afterDraw(String result) { 
    Log.d("response",""+result); 
} 

就是這樣。我希望這將有所幫助。完整代碼Github

Result

+0

能否在此添加航點? –

+0

是的,您也可以添加航點。在結果中,您將獲得一切。 –