how to get polyline from directions?

6,620 views
Skip to first unread message

Josh

unread,
Jul 14, 2010, 9:02:48 AM7/14/10
to Google Maps JavaScript API v3
I'm upgrading my code from v2 to v3 and I used to do something like
the following in order to get a polyline of directions from two
LatLngs:

newDirections.loadFromWaypoints([begin, end],{
getPolyline: true
});

google.maps.Event.addListener(newDirections, "load", function(){
var poly = newDirections.getPolyline();

With v3 (where start/end are LatLngs)

var directionsService = new google.maps.DirectionsService();
var request = {
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};

directionsService.route(request, function(directionsResult,
directionsStatus) {
if (directionsStatus == google.maps.DirectionsStatus.OK) {

path = directionsResult.routes[0].overview_path

console.log($.map(path, function(a){return [[a.lat(),
a.lng()]]}) );


And this is significantly less graceful, doesn't use the API and does
silly mapping with jQuery AND I still don't have a polyline object.

Is there a better way?

Thanks!

Marx Tseng

unread,
Jul 14, 2010, 10:36:42 AM7/14/10
to Google Maps JavaScript API v3

geoco...@gmail.com

unread,
Jul 14, 2010, 11:52:46 AM7/14/10
to Google Maps JavaScript API v3
On Jul 14, 7:36 am, Marx Tseng <marx.z...@gmail.com> wrote:
> Try this.http://marx-tseng.appspot.com/maps/Polyline_from_directions.html

These don't look like documented properties:
polyline.getPath().push(new google.maps.LatLng(item.b, item.c));

I would think this would break the next time the API changes.
(but I could be wrong)

This version uses documented API calls (not that those haven't changed
with v3 also).
http://www.geocodezip.com/V3_Polyline_from_directions.html

-- Larry
> > Thanks!- Hide quoted text -
>
> - Show quoted text -

Marx Tseng

unread,
Jul 14, 2010, 6:00:00 PM7/14/10
to Google Maps JavaScript API v3
Modify the program as follows, this will look a bit simple to use v3
api standards.

directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
path = result.routes[0].overview_path;

$(path).each(function(index, item) {
polyline.getPath().push(item);
bounds.extend(item);
})

polyline.setMap(map);
map.fitBounds(bounds);
}
});

http://marx-tseng.appspot.com/maps/Polyline_from_directions.html

On 7月14日, 下午11時52分, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:
> On Jul 14, 7:36 am, Marx Tseng <marx.z...@gmail.com> wrote:
>
> > Try this.http://marx-tseng.appspot.com/maps/Polyline_from_directions.html
>
> These don't look like documented properties:
> polyline.getPath().push(new google.maps.LatLng(item.b, item.c));
>
> I would think this would break the next time the API changes.
> (but I could be wrong)
>
> This version uses documented API calls (not that those haven't changed
> with v3 also).http://www.geocodezip.com/V3_Polyline_from_directions.html

Josh

unread,
Jul 14, 2010, 4:15:26 PM7/14/10
to Google Maps JavaScript API v3
Larry,

I tried to figure out which part of your example is undocumented but
was able to find everything I looked for -- mind pointing those out?

Thanks




On Jul 14, 11:52 am, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:
> On Jul 14, 7:36 am, Marx Tseng <marx.z...@gmail.com> wrote:
>
> > Try this.http://marx-tseng.appspot.com/maps/Polyline_from_directions.html
>
> These don't look like documented properties:
> polyline.getPath().push(new google.maps.LatLng(item.b, item.c));
>
> I would think this would break the next time the API changes.
> (but I could be wrong)
>
> This version uses documented API calls (not that those haven't changed
> with v3 also).http://www.geocodezip.com/V3_Polyline_from_directions.html

geoco...@gmail.com

unread,
Jul 14, 2010, 6:30:54 PM7/14/10
to Google Maps JavaScript API v3
On Jul 14, 4:15 pm, Josh <josh.m.sha...@gmail.com> wrote:
> Larry,
>
> I tried to figure out which part of your example is undocumented but
> was able to find everything I looked for -- mind pointing those out?

As far as I know, my version used documented API calls. My only point
was that since API v3 is still under development, those may (at least
have in the past) change. Undocumented properties and functions can
change or go away without warning.

-- Larry

bratliff

unread,
Jul 14, 2010, 9:39:49 PM7/14/10
to Google Maps JavaScript API v3
Reply all
Reply to author
Forward
0 new messages