March 28, 2021

Draw direction and by parsing direction API response

 I want to draw direction with waypoints 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.


I need to draw direction from 10.1849092, 76.3753045   to 10.3080271 , 76.3336779 

also I have some waypoints(this means need to reach destination through that points), Here is my waypoints 
10.2269749, 76.3750218
10.2632015, 76.3492569
10.2834370, 76.3420206


For this first we need to generate the request URL, Here is the format

https://maps.googleapis.com/maps/api/directions/json?
origin
=Toronto&destination=Montreal
&key=YOUR_API_KEY

This is is the format of URL without passing waypoints.

https://maps.googleapis.com/maps/api/directions/json?
origin
=Boston,MA&destination=Concord,MA
&waypoints=via:Charlestown,MA|via:Lexington,MA &departure_time=now
&key=YOUR_API_KEY

Here is the final URL from my origin , destination and waypoint locations,

https://maps.googleapis.com/maps/api/directions/json?origin=0.1849092,76.3753045&destination=10.3080271 ,76.3336779&waypoints=10.2269749,76.3750218|10.2632015, 76.3492569|10.2834370, 76.3420206&key=YOUR_API_KEY

Here will be the response format from Google Direction Api,

From this Google Direction Api response we use the value "routes" -> "overview_polyline"-> "points" to drew the direction, 

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


We are  using drawDirectionToStop() to draw the route, I am passing to the value of "points" decodeOverviewPolyLinePonts() for parse the response, Here is the functions


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

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







No comments:

Post a Comment