绘制方向与解析方向apioverview_polyline

Draw direction and by parsing direction api overview_polyline

我想根据 google 方向 Api 的结果用路标绘制方向,由此 URL 我想解析响应并在 google地图.

Google方向ApiURl 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" 的值来取消线路,我将 "points" 的值设为

"overview_polyline" : {
            "points" : "yhd}@madqMD]CiA]eF[_HMaCKg@]]SUIUMq@Cw@Cy@M_AUkAsCqAuA{@Y]w@oAeA}BuAaDaAkCa@{@g@y@eCsCi@g@kAs@o@OuBWwCUs@As@@cBJoAVaI`CiC|@iD~@mBf@gFjB{C`AoBl@eEnA_HxBYHiEj@wDd@uEv@aEv@{BNkA@sACwEGy@?oAHiBVs@P}@\aEpBuHpDuG~CqDbB{FpCeDrAyCv@kIlBwLrCkCl@qF~@cDf@yEx@}BZ[DCSdBWzAWCMvCg@rF_AhAQC_@S}Ak@oAs@qAKg@EiCOuAEi@PqCj@}DBc@?_EMwB?{AJoAAoAgAEoE?aCEkACHv@AZm@XmA`@m@H@FF~@KrAYhAe@xCIhFCxC?jBDf@Bj@ZzDFn@Nt@TlBB~@N`BDd@@Jn@IlCe@t@MBLpKaB`Eu@pEeAxKoCvJ{BvBq@tAm@JT]L}B|@yCv@kIlBwLrCkCl@qF~@cDf@yEx@}BZmCb@gMtB{GbAiHhAeBR_CJaA@mACuG[gBGiABcDHwGRqQl@yANaBZ{Bl@qB~@qAv@aCjByCfCmA|@uBhAwCvAkAd@kBl@sDhA}HbCyH|BeBx@oErB}Br@sFlAoAZu@Zu@\}A`AwAdAa@^m@n@i@x@i@dAe@tAkBpGoAjDgCtGqFfOyErL{@jBsAbCmAtBo@x@s@t@i@f@mE|C?HEJ_DjC{CbCmD`DMLFFLLv@g@f@WTE^AxBJrCRv@L\Lf@^d@p@HZH\Rc@L[@@PD`@J~@HR?@IBET@l@BBG@OH[Fc@dADDAE@eAEGb@IZANCFm@CUACDAHS?_AIs@QAAMZSb@I]I[e@q@g@_@]Mw@MsCSyBK_@@UDg@Vw@f@_@e@oAdAmAp@gChAwAb@qHxBsHfCcAd@sBjAoHrFgAt@}@j@w@b@cBl@eBd@aANuAJ}EBqCEuCFoD`@s@NcAXSHEHQNwG|BXbDYcDwDxAOFEOiAb@cBt@mBrA}@~@cAvAkApB_AfCuAvDy@zBi@tAq@nAk@v@g@h@aAp@wAr@}@ZaATyB\w@B}@@wACs@EoAOuBk@cM{DaAQwAImA?uIZgDBwDCsA@_AF}@LkAX{@ZcCdA{JhEyAl@cEnBkCvAsEnCiBnAcEdCaDtB{D`CaClA_GlCsErBJPkHzCwSjJCk@`GmCzG}CtHcDzMeGlEwBlMcIvA{@MItCkB"
         }

现在我想解析这个并绘制路线图。

我正在使用drawDirectionToStop()绘制路线,我正在传递"points" 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;
}

您可以使用此库在 Google Maps:

上简单地绘制多段线

compile 'com.cs:googlemaproute:1.0.0'

然后实现接口:

implements DrawRoute.onDrawRoute

现在只需像这样绘制多段线:

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

回调监听器

此侦听器 return 状态消息和位置,从 google 接收。

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

就是这样。我希望这将有所帮助。 Github.

上的完整代码