Question

I'm creating a program that would allow users to save a route to a database - however, that's against the terms for Bing Maps, so I am allowing waypoints to be created and saved. I'm not quite sure how to start doing that. I thought I knew how to create the waypoints - but it's not working I'm using Visual Studio - VB.NET/ASP.NET. Would it be recommended to put it in through HTML or Visual Basic? HTML is where all the other Bing coding is located.

  <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: 'AohjORMDA3Vf0mCtDaEsnVHlza-E9AQMlGa0fKhH-6cvoT09Wjhc2RVzCpKG9HbP' });
      }

      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: '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();
      }

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

      }

This is what I have as of right now. This is the code I need to put in for a waypoint - when I have tried putting it in, the directions did not show up:

      function addWaypoint() {
          if (!directionsManager) { createDirectionsManager(); }
          if (directionsManager.getAllWaypoints().length < 2) {
              directionsManager.resetDirections();
              var startWaypoint = new Microsoft.Maps.Directions.Waypoint({ address: 'Bismarck, ND' });
              directionsManager.addWaypoint(startWaypoint);
              var destinationWaypoint = new Microsoft.Maps.Directions.Waypoint({ address: document.getElementById('hometown').value });
              directionsManager.addWaypoint(destinationWaypoint);

          }
          // Insert a waypoint
          directionsManager.addWaypoint(new Microsoft.Maps.Directions.Waypoint({ address: 'Mandan, ND' });
          // Set the element in which the itinerary will be rendered
          directionsManager.setRenderOptions({ itineraryContainer: document.getElementById('directionsItinerary') });
          displayAlert('Calculating directions...');
          directionsManager.calculateDirections();
      }

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

Let's start with this problem. How can I fix this?

Was it helpful?

Solution

I found the answer. The waypoint will load automatically once the form is loaded.

<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: 'AohjORMDA3Vf0mCtDaEsnVHlza-E9AQMlGa0fKhH-6cvoT09Wjhc2RVzCpKG9HbP' });
  }

  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();
      }
  }          

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