none
Howto Create Waypoints from pushpins in layer RRS feed

  • Question

  • I have my pushpins in a layer already.  Now I want them added as waypoints.  Unable to figure out how to do this from the given pushpins.  My code I have that wont work is below.

    var layerpts = layer1.getPrimitives();
    alert(layerpts.length);
    if (layerpts.length > 0) {
         directionsManager.addWaypoint(new Microsoft.Maps.Directions.Waypoint({ location: new Microsoft.Maps.Location(40.748900, -82.517530) }));  <<<<<< this works.. end and start
          for (var x = 0; x < layerpts.length;) {
                 x++;
                 wpoint = layerpts[x].getLocation();
                 alert(wpoint);  <<<<<<<<<<< this shows for example: [MapLocation (40.684294, -82.605373)]
                 wpoint = new Microsoft.Maps.Directions.Waypoint({location: wpoint});  <<<<<< fails
                 directionsManager.addWaypoint(wpoint);
          }
         directionsManager.addWaypoint(new Microsoft.Maps.Directions.Waypoint({ location: new Microsoft.Maps.Location(40.748900, -82.517530) }));  <<<<<< this works.. end and start

    The default example for creating a route works fine, however I need to create my waypoints from exising pushpins in an existing Layer.  I am missing something in the syntax?

    Thursday, March 15, 2018 2:34 PM

Answers

  • Your for loop looks a bit odd:

    for (var x = 0; x < layerpts.length;) {
                 x++;

    This would mean x goes from 1 to the length of points you have which would result in trying to access a cell beyond the limits of the array. Try changing your for loop to this:

    for (var x = 0; x < layerpts.length;x++) {

    Also, I don't see wpoint declared anywhere (no var statement for it). Add "var wpoint;" before the for loop, or add a "var" inline where you are passing a value to it. I believe this would cause an issue if you have strict settings enabled.


    [Blog] [twitter] [LinkedIn]

    Thursday, March 15, 2018 4:52 PM

All replies

  • Your for loop looks a bit odd:

    for (var x = 0; x < layerpts.length;) {
                 x++;

    This would mean x goes from 1 to the length of points you have which would result in trying to access a cell beyond the limits of the array. Try changing your for loop to this:

    for (var x = 0; x < layerpts.length;x++) {

    Also, I don't see wpoint declared anywhere (no var statement for it). Add "var wpoint;" before the for loop, or add a "var" inline where you are passing a value to it. I believe this would cause an issue if you have strict settings enabled.


    [Blog] [twitter] [LinkedIn]

    Thursday, March 15, 2018 4:52 PM
  • Thank you, I know you are correct about x++, however I did declare var, just didn't put all the code in post.

    After changing the x++ the code works as shown below. (for anyone else wondering about this

    VFP does it all

    if (layerpts.length > 0) {
            directionsManager.addWaypoint(new Microsoft.Maps.Directions.Waypoint({ location: new Microsoft.Maps.Location(40.748900, -82.517530) }));
           for (var x = 0; x < layerpts.length; x++) {
                   wpoint = layerpts[x].getLocation();
                   wplat=wpoint.latitude;
                   wplon=wpoint.longitude;
                   wpoint2 = new Microsoft.Maps.Directions.Waypoint({ location: new Microsoft.Maps.Location(wplat, wplon) });
                   directionsManager.addWaypoint(wpoint2);
           }
           directionsManager.addWaypoint(new Microsoft.Maps.Directions.Waypoint({ location: new Microsoft.Maps.Location(40.748900, -82.517530) }));
     }

    Thank you again

    Thursday, March 15, 2018 6:47 PM