Select to view content in your preferred language

Multiple service areas

1262
4
Jump to solution
01-13-2014 05:21 AM
MattTenold
Deactivated User
Hi everyone,

I new to using the ESRI JS API, and I have a hard time working with arrays and the ESRI ServiceAreaTask.   What I would like to do is allow a user to place multiple points and see a 1 and 2 minute response area around each point.

Here is my code

I keep getting the following warning with Firebug: "Graphic is not defined"  I am confused since I am defining Graphic as new.

Any help would be greatly appreciated.

function mapClickHandler(evt){
      map.graphics.clear(); //clear existing graphics   
     
      //define the symbology used to display the results and input point
      var pointSymbol = new esri.symbol.SimpleMarkerSymbol(
        esri.symbol.SimpleMarkerSymbol.STYLE_DIAMOND,
        20,
        new esri.symbol.SimpleLineSymbol(
          esri.symbol.SimpleLineSymbol.STYLE_SOLID,
          new dojo.Color([88,116,152]),
          2
        ),
        new dojo.Color([88,116,152,0.45])
      );     
   //map.graphics.add(new esri.Graphic(evt.mapPoint, pointSymbol)).getDojoShape().moveToBack();
   //alert(evt.mapPoint.toString());
   var geometry = new esri.geometry.Point(evt.mapPoint.x,evt.mapPoint.y,map.spatialReference);
   var graphic = new Graphic(geometry);
   var inPoint = new array();
   for (var i = 1; i < evt.geometry.length; i++) {
      inPoint.push();
   }
   //alert(evt.inPoint.toString());
      //var inPoint = new esri.geometry.Point(evt.mapPoint.x,evt.mapPoint.y,map.spatialReference);
      //var location = new esri.Graphic(inPoint,pointSymbol);
      //map.graphics.add(location);
     
      var features = [];
      features.push(location);
     
      var facilities = new esri.tasks.FeatureSet();
      facilities.features = features;
      params.facilities = facilities;

      //solve
      serviceAreaTask.solve(params, function(serviceAreaSolveResult){
        var serviceAreaSymbol = new esri.symbol.SimpleFillSymbol(
          esri.symbol.SimpleFillSymbol.STYLE_SOLID, 
          new esri.symbol.SimpleLineSymbol(
            esri.symbol.SimpleLineSymbol.STYLE_SOLID,
            new dojo.Color([232,104,80]),
            2
          ),
          new dojo.Color([232,104,80,0.25])
        );

        dojo.forEach(serviceAreaSolveResult.serviceAreaPolygons,function(serviceArea){
          serviceArea.setSymbol(serviceAreaSymbol);
          map.graphics.add(serviceArea);
        });
       
      }, function(err){
        console.log(err.message);
      });
    }

Thanks
0 Kudos
1 Solution

Accepted Solutions
MattTenold
Deactivated User
Never mind I got it to work.   It was my fault. Thanks John.

View solution in original post

0 Kudos
4 Replies
JohnGravois
Deactivated User
hi matt,

your 'Graphic is undefined' error is the result of using an AMD code snippet within a sample that uses the legacy loading pattern (ie. esri.Graphic refers to something here, but not 'Graphic' all by itself.)

in order to solve this problem you are going to have to avoid creating a new array each time someone clicks on the map.

heres a working sample and a description of the logic it uses.

1. create global variables for a variable called 'features' and another called 'facilities'
var map, serviceAreaTask, params, clickpoint, features, facilities;

2. make features an empty array within the function that fires on page load and make facilities a new FeatureSet()
function init() {
  ...
  features = [];
  facilities = new esri.tasks.FeatureSet();
}

3. stop clearing the map graphics each time the map is clicked (because we need more than one to display)
function mapClickHandler(evt) {
  //map.graphics.clear();
}

4. check how many items can be found within the array of graphics called 'features'.  once there are two, call the service area code.
features.push(location);      
      
if (features.length === 2) {      
  facilities.features = features;
  ...


ps. in the future, please consider posting your code within CODE tags.  it helps with formatting.
0 Kudos
MattTenold
Deactivated User
Thanks John for the explanation.  The only thing is I tried the working sample and it does not work for me.  I the two service areas sample working in another browser besides firefox?  I can add points and I set the time to 120 seconds but all I see are the points I added.

Thanks
Matt
0 Kudos
MattTenold
Deactivated User
Never mind I got it to work.   It was my fault. Thanks John.
0 Kudos
JohnGravois
Deactivated User
glad to hear it matt. please consider marking this thread as 'answered'.
0 Kudos