plot line on the road google direction api

92 views
Skip to first unread message

Saboor Awan

unread,
Jan 6, 2012, 9:04:54 AM1/6/12
to google-map...@googlegroups.com
Hi,
I am using google direction api in WP7 app, it returns a json format with steps to move from start to destination.

My API Call 


my app parse the json, pick the step's start_location and end_location and plot a straight line on the map

but google draws a line on the road from 4th step to 5th step.(check maps.google.com link)
how do i know plot a line on the road, as you can check there is no points given for that.

a think the polyline contain encrypted points for that, but i am not confirmed.....






geoco...@gmail.com

unread,
Jan 6, 2012, 9:20:33 AM1/6/12
to Google Maps JavaScript API v3
On Jan 6, 6:04 am, Saboor Awan <sab...@folio3.com> wrote:
> Hi,
> I am using google direction api in WP7 app, it returns a json format with
> steps to move from start to destination.
>
> My API Call
https://maps.googleapis.com/maps/api/directions/json?origin=24.874742...
>
> maps.google.com result :  my api call for location<

This is not the result of a call to the google maps direction web
service, it is a link to google maps:
http://maps.google.com/maps%3Fsaddr%3D24.874742,67.039934%26daddr%3D24.884685,67.056921%26hl%3Den%26ll%3D24.881337,67.048659%26spn%3D0.01314,0.022724%26sll%3D24.874842,67.040132%26sspn%3D0.01314,0.022724%26geocode%3DFfaOewEdvvL-Aw%253BFc21ewEdGTX_Aw%26vpsrc%3D0%26mra%3Dls%26t%3Dm%26z%3D16

If I look in the JSON result returned I see:

"polyline" : {
"points" : "aqyvCq{dxKuBPsBP"
},

That is an encoded polyline.
Decoder here:
http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/decode.html

Decoding it results in a set of lat/long coordinates:
24.875850000000003, 67.04073000000001
24.876440000000002, 67.04064000000001
24.87702, 67.04055000000001

Larry

>
> my app parse the json, pick the step's start_location and end_location and
> plot a straight line on the map
>
> but google draws a line on the road from 4th step to 5th step.(check
> maps.google.com link)
> how do i know plot a line on the road, as you can check there is no points
> given for that.
>
> a think the *polyline* contain encrypted points for that, but i am not
> confirmed.....

Saboor Awan

unread,
Jan 9, 2012, 3:05:05 AM1/9/12
to google-map...@googlegroups.com
thanks for the help. i got the polyline decoder for C#
private static List<Location> DecodePolylinePoints(string encodedPoints) 
            {
                if (encodedPoints == null || encodedPoints == "") return null;
                List<Location> poly = new List<Location>();
                char[] polylinechars = encodedPoints.ToCharArray();
                int index = 0;

                int currentLat = 0;
                int currentLng = 0;
                int next5bits;
                int sum;
                int shifter;

               try
                {
                    while (index < polylinechars.Length)
                    {
                        // calculate next latitude
                        sum = 0;
                        shifter = 0;
                        do
                        {
                            next5bits = (int)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 = (int)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);
                        Location p = new Location();
                        p.Latitude = Convert.ToDouble(currentLat) / 100000.0;
                        p.Longitude = Convert.ToDouble(currentLng) / 100000.0;
                        poly.Add(p);

                    } 
                    return poly;
                }
                catch (Exception ex)
                {
                    // log it
                    throw;
                }
            }
Reply all
Reply to author
Forward
0 new messages