pbugnion / gmaps

Google maps for Jupyter notebooks
https://jupyter-gmaps.readthedocs.io/en/stable/
Other
760 stars 147 forks source link

Displaying directions json from google maps API #323

Open fjanoos opened 4 years ago

fjanoos commented 4 years ago

I was trying to plot the directions json that I get directly from the google maps API - since this api allows more options than gmaps.directions_layer (esp. around departure and arrival time and traffic models).

This call to the API returns the directions as a bunch of steps - as per:

'steps': [{'distance': {'text': '249 ft', 'value': 76},
      'duration': {'text': '1 min', 'value': 21},
      'end_location': {'lat': 40.7281073, 'lng': -74.0500936},
      'html_instructions': 'Head <b>west</b> on <b>Pavonia Ave</b> toward <b>Brunswick St</b>',
      'polyline': {'points': 'cuqwFnv}bMCd@MjC'},
      'start_location': {'lat': 40.7280249, 'lng': -74.0492024},
      'travel_mode': 'DRIVING'},
     {'distance': {'text': '0.4 mi', 'value': 698},
      'duration': {'text': '3 mins', 'value': 207},
      'end_location': {'lat': 40.721961, 'lng': -74.0513588},
      'html_instructions': 'Turn <b>left</b> onto <b>Brunswick St</b>',
      'maneuver': 'turn-left',
      'polyline': {'points': 'uuqwF`|}bMxCTlCTp@Hv@F^DdAHfAL`@D`@DfAL`@?nBLjCThCXJRPH^VZR'},
      'start_location': {'lat': 40.7281073, 'lng': -74.0500936},
      'travel_mode': 'DRIVING'},
     {'distance': {'text': '0.9 mi', 'value': 1427},
      'duration': {'text': '5 mins', 'value': 287},
      'end_location': {'lat': 40.7173147, 'lng': -74.0356193},
      'html_instructions': 'Turn <b>left</b> onto <b>Christopher Columbus Dr</b>',
      'maneuver': 'turn-left',
      'polyline': {'points': 'gopwF~c~bM@O@K?M?M@M?U?M?M?OBO@OBO@Ej@mCZwAdA{EZyAJi@r@aDn@qCJe@h@gC^}AbB_Ij@qCXsA^cB`AqE|AgHZ{AjAgFv@uEHq@Jw@'},
      'start_location': {'lat': 40.721961, 'lng': -74.0513588},
      'travel_mode': 'DRIVING'},

I would like to render these directions in the jupyter map using gmaps.

If I just build a gmap.drawing_layer with gmaps.Line objects using the start_location and end_location from the json above - this draws straight lines which misses turns and curves in the roads.

Is there an alternative way to display the directions json that comes back from google using gmaps ?

Thanks

NOTE: alternatively - would it be possible to update gmaps.directions_layer to support all these additional options ?

mspoorendonk commented 4 years ago

Formulating the above a bit more lowlevel: It would be great if the Polyline layer was supported in order to plot the polyline strings from that API response directly on the map.

I know that one could also use the Directions layer for that, however I would like to store and plot a couple of hundred of those lines in a single layer. The functionality would be very similar to that of the polygon in the drawing_layer(). The difference being that the polygon is closed and filled while the polyline is not.

See also this google reference: https://developers.google.com/maps/documentation/javascript/shapes