Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm going to assume that the following is all true:</p> <pre><code>var map = new google.maps.Map(document.getElementById('map_canvas'), { zoom: 8, center: new google.maps.LatLng(/* center of map */), mapTypeId: google.maps.MapTypeId.ROADMAP }), directions = new google.maps.DirectionsService(), displayer = new google.maps.DirectionsRenderer({ draggable: true }); displayer.setMap(map); directions.route({ origin: new google.maps.LatLng(/* start point */), destination: new google.maps.LatLng(/* end point */), travelMode: google.maps.DirectionsTravelMode.DRIVING }, function (result) { displayer.setDirections(result); }); </code></pre> <p>Basically, this just assumes that the start- and end-points have already been chosen and the default route has already been drawn. (<strong>NOTE:</strong> The <code>DirectionsRenderer</code> has been initialized with <code>draggable: true</code>.)</p> <p>When the user changes the route, the application fires a <code>directions_changed</code> event. We can track that like so:</p> <pre><code>google.maps.event.addListener(displayer, 'directions_changed', some_method); </code></pre> <p>Something else <em>also</em> happens when the change the route: a new <code>waypoint</code> is created. Here's how we get at all the waypoints:</p> <pre><code>var some_method = function () { var waypoints = displayer.directions.route[0].legs[0].via_waypoint; }; </code></pre> <p>The variable <code>waypoints</code> is an array of objects describing the stops the route makes on its way to the destination. (Note that more assumptions have been made: you're using <code>route[0]</code>, <code>legs[0]</code>, etc.)</p> <p>Each <code>waypoint</code> object has a <code>location</code> property, which contains the latitude and longitude (available in <code>location.wa</code> and <code>location.ya</code> respectively, for some reason). So we can tell the application, every time the user changes the route, to rotate through (and store) all the lats and longs of the current waypoints. Once you have those, you can decide how you want to store them (AJAX to a server-side page that stores them in a database, <code>localStorage</code>, etc.)</p> <p>Then!</p> <p>The next time you load this page, you can grab those waypoints from the storage and initialize the route like so:</p> <pre><code>directions.route({ origin: new google.maps.LatLng(/* start point */), destination: new google.maps.LatLng(/* end point */), travelMode: google.maps.DirectionsTravelMode.DRIVING, waypoints: [ { location: new google.maps.LatLng(/* lat/lng go here */) } // repeat this as necessary ] }, function (result) { displayer.setDirections(result); }); </code></pre> <p>This should maintain the new route that the user chose. One final note: I left <strong>a lot</strong> out of this answer, like <em>how</em> they save it, how you know which user wants which route, etc. But the foundation is there. Godspeed.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload