How to snap polyline to road?

3,996 views
Skip to first unread message

Doman

unread,
May 2, 2011, 6:46:41 AM5/2/11
to google-map...@googlegroups.com
Hi!

I want to have polyline combined from parts of lines which are snapped to road and "free" polilines. I want to do this in static mode: I mean that I can put coordinates into code. There are examples how to do it in v2 (but in active mode: user click markers and then api search the shortest path) but how to do it in v3?

Best Regards
Doman

geoco...@gmail.com

unread,
May 2, 2011, 11:07:58 AM5/2/11
to Google Maps JavaScript API v3
Use the directions service:
http://code.google.com/apis/maps/documentation/javascript/services.html#Directions

-- Larry

>
> Best Regards
> Doman

Doman

unread,
May 2, 2011, 8:47:45 PM5/2/11
to google-map...@googlegroups.com
Thanks. But next question is how to save result of direction service (I assume that it is DirectionsRoute object) as a part polyline. I have this situation that I have to combine shreds of polylines snaped to road and ahreds of "free" polylines.

Or at least is it possible somehow to change colour, visibility and opacity of line related to DirectionsRoute?

BR
Doman

en4ce

unread,
May 3, 2011, 6:45:13 AM5/3/11
to Google Maps JavaScript API v3
1st, just save the lat lng results to a database (create an array
which hold the lat lngs and save them within another function)
2nd, you can set what ever you want to a polyline (color, opacity,
thickness etc)

Doman

unread,
May 3, 2011, 8:45:22 AM5/3/11
to google-map...@googlegroups.com
OK problem solved!

It is easy. Solution needs extracting coordinates from directions service and then create polyline accordingly. In below example start and end coordinates are beginning and end of path searched by directions service and ble is coordinates which is later attached to polyline without in "free" mode :)


            function calcRoute() {
                var request = {
                origin:start,
                destination:end,
                travelMode: google.maps.TravelMode.WALKING
                };
               

                directionsService.route(request, function(response, status) {
                    if (status == google.maps.DirectionsStatus.OK)
                    {
                       
                        var steps = response.routes[0].legs[0].steps;
                       
                        for(var step = 0; step < steps.length; step++)
                        {
                            polylineOptions = {
                                    map: mapa,
                                    strokeColor: "#FF0000",
                                    strokeOpacity: 0.7,
                                    strokeWeight: 2,
                                    path: steps[step].path
                            }
                        police = new google.maps.Polyline(polylineOptions);
                        }
                       
                        police.getPath().push(ble);
                       
                    }
                   
                });


Similar case: it is possibla to copy path created in plain google maps to path format using in google maps api?

BR
Doman
Reply all
Reply to author
Forward
0 new messages