3
votes

I want to draw direction with waypoits based on the result of google direction Api, From this URL I want to parse the response and draw the route on the google map.

Google Direction Api URl 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|

From the result I am taking the value of "routes" -> "overview_polyline"-> "points" to deaw the line, I am having "points" value as

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

Now I want to parse this and draw the route map.

2
Refer this it will help you journaldev.com/13373/…RajatN

2 Answers

0
votes

I am using drawDirectionToStop() to draw the route, I am passing to the value of "points" decodeOverviewPolyLinePonts() for parse,

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
votes

You can use this library to simply draw polylines on Google Maps:

compile 'com.cs:googlemaproute:1.0.0'

Then Implement Interface:

implements DrawRoute.onDrawRoute

Now Simply draw polyline like this:

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

Callback listener

This listener return status message and locations, receive from google.

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

That's it. I Hope this will help. Complete Code on Github.

Result