2

I want to use ArcGis Javascript API for directions with the users current loaction as the start location, and a predetermined location as the stop destination. All the examples I can find use a widget for start and stop locations.

How can I set a predetermined location for the destination, and a current location for the start rather than using the widgets?

5
  • Can you create a RouteTask with your specified locations, rather than use the widget?
    – kes
    Commented Jul 26, 2015 at 19:52
  • I'm new at ArcGis. How can set current location as start location?
    – yyy
    Commented Jul 27, 2015 at 6:53
  • If you know how to take a point and set a start location, you should be able to use HTML5's Geolocation to derive a position and fill in your start position.
    – Branco
    Commented Aug 4, 2015 at 12:12
  • Actually I don't know that too :(
    – yyy
    Commented Aug 4, 2015 at 13:48
  • If you can add some of what you've tried, someone may be able to better help you along to a solution. Please edit your question with any relevant code, methods, anything that you may have tried.
    – Branco
    Commented Aug 6, 2015 at 14:33

1 Answer 1

2
+50

I cobbled together a few chunks of code that should help using the RouteTask suggested by @kes. I took code chunks from this ESRI Geolocation example and this ESRI RouteTask example.

My example uses a mouse click to set the destination location, and the geolocation results to set the starting point. You can use these principles to customize the code chunks for your needs.

This is not tested, nor is it complete code... It is here to illustrate the process in which something like this would be accomplished. Whatever you end up putting together, it would be great if you'd post the code to aid others.

If this is confusing, I suggest you take a look more through ESRI's JS API Documentation and through this info on callback functions.

 //in global namespace 
 var currentMapPoint;
 var lastStop;
 var routeSymbol;

 //in the  require function
 //be sure you import the modules
//require(["esri/tasks/RouteTask", "esri/tasks/RouteParameters"], RouteTask, RouteParameters) etc...


  //this is only adding a click even handler
 //your implementation will need to be much better than this
 //I am assuming you'll want your users to have some flexibility
 map.on("onClick", clickFunc);

 function clickFunc(evt) {
      if( navigator.geolocation ) {  

        //adjust your global with a new point for where your mouse clicked
        currentMapPoint = new Point(evt.mapPoint.x, evt.mapPoint.y);

        //set up the geolocation and call back
        navigator.geolocation.getCurrentPosition(routeLocation, locationError);
      } else {
        alert("Browser doesn't support Geolocation. Visit http://caniuse.com to see browser support for the Geolocation API.");
      }
    }

    function locationError(error) {
      //error occurred so stop watchPosition
      if( navigator.geolocation ) {
        navigator.geolocation.clearWatch(watchId);
      }
      switch (error.code) {
        case error.PERMISSION_DENIED:
          alert("Location not provided");
          break;

        case error.POSITION_UNAVAILABLE:
          alert("Current location not available");
          break;

        case error.TIMEOUT:
          alert("Timeout");
          break;

        default:
          alert("unknown error");
          break;
      }
    }

    function routeLocation(location) {
      //create your point geometry obejct
      var destinationPoint = new Point(location.coords.longitude, location.coords.latitude);

     //create your new route take
     //you'll need to make sure your server or service is capable of routing tasks
      routeTask = new RouteTask("YOUR ROUTING SERVICE URL");

    //setup the route parameters
    routeParams = new RouteParameters();
    routeParams.stops = new FeatureSet();
    routeParams.outSpatialReference = {
      "wkid" : 102100 //or whatever spatial ref your map is in
    };

    //set your call back handlers
    routeTask.on("solve-complete", showRoute);
    routeTask.on("error", errorHandler);               

    //define the symbology used to display the route
    //start symbol  
    startSymbol = new SimpleMarkerSymbol().setStyle(SimpleMarkerSymbol.STYLE_CROSS).setSize(15);
    startSymbol.outline.setWidth(4);
     var start = map.graphics.add(new Graphic(currentMapPoint, startSymbol));
     routeParams.stops.features.push(start);

    //stop symbol
    stopSymbol = new SimpleMarkerSymbol().setStyle(SimpleMarkerSymbol.STYLE_CROSS).setSize(15);
    stopSymbol.outline.setWidth(4);
     var stop = map.graphics.add(new Graphic(destinationPoint, stopSymbol));
     routeParams.stops.features.push(stop);

    //route symbol
    routeSymbol = new SimpleLineSymbol().setColor(new dojo.Color([0, 0, 255, 0.5])).setWidth(5);

      //verify we have 2 points added
      if (routeParams.stops.features.length >= 2) {
        routeTask.solve(routeParams);
        lastStop = routeParams.stops.features.splice(0, 1)[0];
      }

    }

    //Adds the solved route to the map as a graphic
    function showRoute(evt) {
      map.graphics.add(evt.result.routeResults[0].route.setSymbol(routeSymbol));
    }

    //Displays any error returned by the Route Task
    function errorHandler(err) {
      alert("An error occured\n" + err.message + "\n" + err.details.join("\n"));

      routeParams.stops.features.splice(0, 0, lastStop);
      map.graphics.remove(routeParams.stops.features.splice(1, 1)[0]);
    }



      var symbol = new SimpleMarkerSymbol(
        SimpleMarkerSymbol.STYLE_CIRCLE, 
        12, 
        new SimpleLineSymbol(
          SimpleLineSymbol.STYLE_SOLID,
          new Color([210, 105, 30, 0.5]), 
          8
        ), 
        new Color([210, 105, 30, 0.9])
      );
      graphic = new Graphic(pt, symbol);
      map.graphics.add(graphic);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.