-2

I am using Google's JAVA client API to get the route and i am sending the request like this,

   DirectionsRoute[] routes = DirectionsApi.newRequest(context)
                              .mode(TravelMode.DRIVING)
                              .origin(start).destination(end)
                              .waypoints(wayPoints).await();

It is returning route also but if i plot that route it is not plotting on actual route instead it just takes straight line as shown in image.enter image description here How to fix it?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Manjunath M
  • 588
  • 1
  • 6
  • 30

2 Answers2

1

Actually i made a mistake after that request i am getting the result with routes and but it also contains the encoder polyline object which consist of the array of lat lang, Once we decode that then we'll get all the points so that we can get the route correctly. The result consist of array of routes each element consist of array of legs(route details between two points) and each leg consist of steps finally each step has encoded polyline in order to get the correct latlang you should decode that polyline and use it.

Manjunath M
  • 588
  • 1
  • 6
  • 30
0

thank you very much, you have helped me a lot, I am currently working with xamarin and use this method to decode polyline.

        private List<LatLng> DecodePolyline(string encodedPoints)
    {
        if (string.IsNullOrWhiteSpace(encodedPoints))
        {
            return null;
        }

        int index = 0;
        var polylineChars = encodedPoints.ToCharArray();
        var poly = new List<LatLng>();
        int currentLat = 0;
        int currentLng = 0;
        int next5Bits;

        while (index < polylineChars.Length)
        {
            // calculate next latitude
            int sum = 0;
            int shifter = 0;

            do
            {
                next5Bits = polylineChars[index++] - 63;
                sum |= (next5Bits & 31) << shifter;
                shifter += 5;
            }
            while (next5Bits >= 32 && index < polylineChars.Length);

            if (index >= polylineChars.Length)
            {
                break;
            }

            currentLat += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);

            // calculate next longitude
            sum = 0;
            shifter = 0;

            do
            {
                next5Bits = polylineChars[index++] - 63;
                sum |= (next5Bits & 31) << shifter;
                shifter += 5;
            }
            while (next5Bits >= 32 && index < polylineChars.Length);

            if (index >= polylineChars.Length && next5Bits >= 32)
            {
                break;
            }

            currentLng += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);

            var mLatLng = new LatLng(Convert.ToDouble(currentLat) / 100000.0, Convert.ToDouble(currentLng) / 100000.0);
            poly.Add(mLatLng);
        }

        return poly;
    }