Find closest facilities

A closest facility task allows you to find facilities that are closest to a given incident using ArcGIS Network Analyst services.

A Network Analyst service contains one or more network analysis layers. ArcGIS provides different types of network analysis layers, such as route, service area, closest facility, and so on, depending on the type of analysis performed. A closest facility task relies on a closest facility analysis layer.

Learn about creating a network analysis service

Finding the closest hospital to an accident, the closest police cars to a crime scene, and the closest store to a customer's address are examples of closest facility problems. When finding closest facilities, specify how many to find and whether the travel direction is toward or away from the facilities. Once you've found the closest facilities, you can display the best route to or from them, return the travel cost for each route, and display directions to each facility. Additionally, you can specify an impedance cutoff beyond which a facility should not be searched. For example, you can set up a closest facility problem to search for hospitals within a 15-minute drive to an accident. Hospitals that take longer than 15 minutes to reach will not be included in the results.

Create a closest facility task

To instantiate a closest facility task, provide a URL to the REST resource that represents a closest facility layer in a Network Analyst service. If the service is secured, provide the credentials that can be used to access the service. The following code snippet shows how to create a closest facility task for the closest facility layer in the San Diego, CA, service on an ArcGIS Online sample server:

// Construct a ClosestFacilityTask
static const QString urlNaSanDiego = 
m_task = new EsriRuntimeQt::ClosestFacilityTask(urlNaSanDiego, this);

Prepare the input

Provide input to the closest facility task using an object of the ClosestFacilityTaskParameters class. Instantiate a new ClosestFacilityTaskParameters object and modify its properties to provide the input.

// Construct a ClosestFacilitiesParameters
EsriRuntimeQt::ClosestFacilityTaskParameters* parameters = new EsriRuntimeQt::ClosestFacilityTaskParameters(this);

The following sections describe some of the inputs you can provide to a closest facility task.


You can specify a cutoff value for the impedance beyond which facilities should be not found. For instance, while locating the closest hospitals from the site of an accident, a cutoff value of 15 minutes indicates that the service should search for the closest hospital within 15 minutes of the accident. If the closest hospital is 17 minutes away, no hospitals return in the closest facility search. A cutoff value is especially useful when searching for multiple facilities.

// Set the default breaks

Facility count

Sometimes you may want to find multiple closest facilities from an incident, for example, the nearest three fire stations to a fire. You can do this by setting the setDefaultTargetFacilityCount property.

// Set the default target facility count


Facilities represent locations that can serve as the starting point or ending point of a closest facility analysis, for example, a fire station or hospital.

Facility features

You can create an array of objects representing facility features. You can then assign these features to the parameter object using setFacilities.

// Create Graphics for each facility
EsriRuntimeQt::SimpleMarkerSymbol facilitiesSymbol(QColor(64, 224, 208), 18, 
facilitiesSymbol.setOutline(EsriRuntimeQt::SimpleLineSymbol(QColor("black"), 0.5, 
QMap<QString,QVariant> attributes;
attributes.insert("Name", QVariant(QString("Facility" + QString::number(m_facilities.size() + 1))));

EsriRuntimeQt::Graphic* graphic = new EsriRuntimeQt::Graphic(mapPoint, facilitiesSymbol, attributes, this);

m_graphicsLayer->addGraphic(new EsriRuntimeQt::Graphic(mapPoint, 
    EsriRuntimeQt::TextSymbol(QColor("black"), 8, QString::number(m_facilities.size()+1))));

// Create the facilities features
EsriRuntimeQt::NAFeaturesAsFeature* facilitiesFeatures = new EsriRuntimeQt::NAFeaturesAsFeature(this);

// add the features


Incidents also represent locations that can serve as the starting point or ending point of a closest facility analysis, for example, a fire or a traffic accident.

Incident features

You can create an array of objects representing incident features in the same way as discussed above for facility features. You can then assign these features using setIncidents.

// Set the incidents (represented as features)

Travel direction

You can find facilities by accumulating impedance in the direction away from the facility toward the incident, or in the reverse direction toward the facility from the incident. This gives different results on a network with one-way restrictions and different impedances based on the travel direction. The direction you choose depends on the nature of your analysis.

The closest facility for a fire response, for example, should be calculated away from the fire station toward the fire since it's imperative to get fire trucks and personnel quickly to the location of the fire. Alternatively, the closest facility for a traffic accident should choose the opposite direction since the urgent part of the trip for an incoming patient is going to the hospital.

// Set the travel direction

Drive directions

The task can also return turn-by-turn driving directions for the route if you enable setReturnDirections on ClosestFacilityTaskParameters. You can specify the distance units to use (miles, kilometers, and so on) through the setDirectionsLengthUnits property, and, depending on the languages supported by the service, you can also specify which language to use through the setDirectionsLanguage property.

The following code snippet requests driving directions in French and uses kilometers as the distance units:

// Set how the directions should appear

Executing a closest facility task

Once you initialize a ClosestFacilityTaskParameters object with the desired input, find the closest facility by calling the solve method.

// Asynchronously solve the closest facility task

Retrieve results

The closest facility task emits the EsriRuntimeQt::closestFacilityTaskComplete signal when the asynchronous solve operation completes. The operation's results are contained in a ClosestFacilityResult object. The route's geometry is returned as a graphic in the list of routes.

As shown in the following code, the onClosestFacilityTaskComplete method retrieves each route graphic from the result, collects the directions, applies a symbol to it, and adds it to a graphics layer:

// Slot to handle closestFacilityTaskComplete signal
void ClosestFacility::onClosestFacilityTaskComplete(EsriRuntimeQt::ClosestFacilityResult* result)
  QList<EsriRuntimeQt::Route> routes = result->routes();
  QMap<QString, QVariant> attributes;

  foreach (EsriRuntimeQt::Route route, routes)
    QList<EsriRuntimeQt::RoutingDirection> directions = route.routingDirections();
    int stepNum = 1;
    foreach (EsriRuntimeQt::RoutingDirection dir, directions)
      attributes.insert(QString::number(stepNum++), QVariant(dir.text()));
    EsriRuntimeQt::SimpleLineSymbol routeSymbol(
        QColor(0, 0, 205), 3, EsriRuntimeQt::SimpleLineSymbolStyle::Dash);

    EsriRuntimeQt::Graphic* routeGraphic = new Graphic(route.route()->geometry(), 
        routeSymbol, attributes, m_graphicsLayer.minDrawOrder() - 1, this);

    qint64 routeID = m_graphicsLayer->addGraphic(routeGraphic);