Selection of Closest Facility on Map Load

1706
3
Jump to solution
03-10-2014 11:40 AM
SaurabhDasgupta
New Contributor II
Hi,

I am finding the route for the Closest Facility from a user defined location. Able to do it successfully and the route is drawn as required, but however I want to select the closest facility as well after the route is drawn. Please find the screen shot for the route display to the closest faclity from the user's location.

Is it possible to do it and can anybody help me with the code for selection for the closest facility. Please find the code below...

       // update the status message
     self.statusMessageLabel.text = @"Finding closest facilities";

     // if we have a sketch layer on the map, remove it
     if ([self.mapView.mapLayers containsObject:self.sketchLayer]) {
     [self.mapView removeMapLayerWithName:self.sketchLayer.name];
     self.mapView.touchDelegate = nil;
      self.sketchLayer = nil;

     //also disable the sketch control so that user cannot sketch
     self.sketchModeSegCtrl.selectedSegmentIndex = -1;
     for (int i =0; i<self.sketchModeSegCtrl.numberOfSegments; i++) {
     [self.sketchModeSegCtrl setEnabled:NO forSegmentAtIndex:i];
     }

     }

     //retrieves the default parameters for the closest facility task from the server
     //the caOp property will keep tract of the operation in case we need to cancel it at any point.
     self.cfOp = [self.cfTask retrieveDefaultClosestFacilityTaskParameters];

     //showing activity indicator
     [self.activityAlertView show];

     self.mapView.touchDelegate = self;


Regards,
Saurabh.
0 Kudos
1 Solution

Accepted Solutions
SuganyaBaskaran1
Esri Contributor
The routeGraphic has an attribute 'FacilityID' which is the ObjectID of the facilities layer you used for the task.
In -(void)closestFacilityTask: operation:didSolveClosestFacilityWithResult: method, you can query for that ObjectID as follows:
//iterate through the closest facility results array in the closestFacilityTaskResult returned by the task     for (AGSClosestFacilityResult *cfResult in closestFacilityTaskResult.closestFacilityResults) {         if (cfResult) {            //First, add route graphic to map           cfResult.routeGraphic.symbol = [self routeSymbol];    [self.graphicsLayer addGraphic:cfResult.routeGraphic];        //Then, query to select the closest facility   AGSQuery *query = [AGSQuery query];   query.where = [NSString stringWithFormat:@"OBJECTID = %@",[cfResult.routeGraphic attributeAsStringForKey:@"FacilityID"]];   [self.facilitiesLayer selectFeaturesWithQuery:query selectionMethod:AGSFeatureLayerSelectionMethodAdd];          self.facilitiesLayer.queryDelegate = self;          }     }


This would select the closest facility.

View solution in original post

0 Kudos
3 Replies
SaurabhDasgupta
New Contributor II
Any Updates??

~Saurabh.
0 Kudos
SaurabhDasgupta
New Contributor II
Hi,

I am finding the route for the Closest Facility from a user defined location. Able to do it successfully and the route is drawn as required, but however I want to select the closest facility as well after the route is drawn. Please find the screen shot for the route display to the closest faclity from the user's location.

Is it possible to do it and can anybody help me with the code for selection for the closest facility. Please find the code below...

       // update the status message
     self.statusMessageLabel.text = @"Finding closest facilities";

     // if we have a sketch layer on the map, remove it
     if ([self.mapView.mapLayers containsObject:self.sketchLayer]) {
     [self.mapView removeMapLayerWithName:self.sketchLayer.name];
     self.mapView.touchDelegate = nil;
      self.sketchLayer = nil;

     //also disable the sketch control so that user cannot sketch
     self.sketchModeSegCtrl.selectedSegmentIndex = -1;
     for (int i =0; i<self.sketchModeSegCtrl.numberOfSegments; i++) {
     [self.sketchModeSegCtrl setEnabled:NO forSegmentAtIndex:i];
     }

     }

     //retrieves the default parameters for the closest facility task from the server
     //the caOp property will keep tract of the operation in case we need to cancel it at any point.
     self.cfOp = [self.cfTask retrieveDefaultClosestFacilityTaskParameters];

     //showing activity indicator
     [self.activityAlertView show];

     self.mapView.touchDelegate = self;


Regards,
Saurabh.


Still no thoughts??
0 Kudos
SuganyaBaskaran1
Esri Contributor
The routeGraphic has an attribute 'FacilityID' which is the ObjectID of the facilities layer you used for the task.
In -(void)closestFacilityTask: operation:didSolveClosestFacilityWithResult: method, you can query for that ObjectID as follows:
//iterate through the closest facility results array in the closestFacilityTaskResult returned by the task     for (AGSClosestFacilityResult *cfResult in closestFacilityTaskResult.closestFacilityResults) {         if (cfResult) {            //First, add route graphic to map           cfResult.routeGraphic.symbol = [self routeSymbol];    [self.graphicsLayer addGraphic:cfResult.routeGraphic];        //Then, query to select the closest facility   AGSQuery *query = [AGSQuery query];   query.where = [NSString stringWithFormat:@"OBJECTID = %@",[cfResult.routeGraphic attributeAsStringForKey:@"FacilityID"]];   [self.facilitiesLayer selectFeaturesWithQuery:query selectionMethod:AGSFeatureLayerSelectionMethodAdd];          self.facilitiesLayer.queryDelegate = self;          }     }


This would select the closest facility.
0 Kudos