Question

I was wondering if when coding, there was a way to make it so the users can make it so there are multiple waypoints. Right now, I'm getting an automatic waypoint 4 miles away from the starting waypoint that the users can edit.

This is the code I have:

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
  <script type="text/javascript">
      var map = null;
      var directionsManager;
      var directionsErrorEventObj;
      var directionsUpdatedEventObj;

      function getMap() {
          map = new Microsoft.Maps.Map(document.getElementById('myMap'), { credentials: 'myKey' });
      }

      function createDirectionsManager() {
          var displayMessage;
          if (!directionsManager) {
              directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);
              displayMessage = 'Directions Module loaded\n';
              displayMessage += 'Directions Manager loaded';
          }
          alert(displayMessage);
          directionsManager.resetDirections();
          directionsErrorEventObj = Microsoft.Maps.Events.addHandler(directionsManager, 'directionsError', function (arg) { alert(arg.message) });
          directionsUpdatedEventObj = Microsoft.Maps.Events.addHandler(directionsManager, 'directionsUpdated', function () { alert('Directions updated') });
      }

      function createDrivingRoute() {
          if (!directionsManager) { createDirectionsManager(); }
          directionsManager.resetDirections();
          // Set Route Mode to driving 
          {
              directionsManager.setRequestOptions({ routeMode: Microsoft.Maps.Directions.RouteMode.driving });
              var startWaypoint = new Microsoft.Maps.Directions.Waypoint({ address: '7500 University Dr., Bismarck, ND' });
              directionsManager.addWaypoint(startWaypoint);
              var destinationWaypoint = new Microsoft.Maps.Directions.Waypoint({ address: document.getElementById('hometown').value });
              directionsManager.addWaypoint(destinationWaypoint);
              // Set the element in which the itinerary will be rendered
              directionsManager.setRenderOptions({ itineraryContainer: document.getElementById('directionsItinerary') });
              alert('Calculating directions...');
              directionsManager.calculateDirections();
          }
          // Insert a waypoint
          directionsManager.addWaypoint(new Microsoft.Maps.Directions.Waypoint({ address: 'Bismarck, ND'}), 1);
          // Set the element in which the itinerary will be rendered
          directionsManager.setRenderOptions({ itineraryContainer: document.getElementById('directionsItinerary') });
          alert('Calculating directions...');
          directionsManager.calculateDirections();
      }

      function createDirections() {
          if (!directionsManager) {
              Microsoft.Maps.loadModule('Microsoft.Maps.Directions', { callback: createDrivingRoute });
          }
          else {
              createDrivingRoute();
          }
      }          


  </script>

Is there a way I can make it so the user decides when to add a waypoint - so that the waypoint wouldn't be automatic and the user can decide to add multiple waypoints?

Thanks in advance.

Was it helpful?

Solution

Yes. You have to create a UI that handles this rather than automatically choosing random waypoints. Then you just have to use the addWaypoint method to add the waypoint to the directions manager.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top