Often you may want to get directions starting from a user's actual current position instead of from another fixed Location. The following code snippet gives an example on how to implement this.
// Setting styling options to the route path.miDirectionsRendererInstance.setOptions({ strokeColor:'#bada55', strokeWeight:10,});// Latitude and longitude position of the route destination.// again, you'll likely want to retrieve this from a location// location.properties.anchor.coordinates, and then access the array directly.constrouteDestination= { lat:57.058230237700194, lng:9.951134229974498};// enableHighAccuracy is a boolean value that indicates that the application would like to receive the best possible results.// timeout represents the maximum length of time (milliseconds) the device is allowed to take in order to return a position.constoptions= { enableHighAccuracy:true, timeout:5000}// Creates origin with the users latitude and longitude. Then sets the route from this position to the route destination.functiongetRoute(pos) {constcoords=pos.coords;constorigin= { lat:coords.latitude, lng:coords.longitude }directionsServiceInstance.getRoute({ origin: origin, destination: routeDestination }).then(function (res) {if (res ===undefined) {console.log('Error: Route is undefined.') } else {console.log('Route', res);miDirectionsRendererInstance.setRoute(res); } });}// Error handling function.functionerrorHandler(err) {console.log('Error: ', err)}// Gets users device current position and sets route with additional options. If any errors, errorHandler will trigger.navigator.geolocation.getCurrentPosition(getRoute, errorHandler, options);
Further details on how user positioning works, and how to display it, can be found here.
This results in directions queries originating from the user's current location.