View on GitHub Sample viewer app

Find routes from several locations to the respective closest facility.

Image of find closest facility static

Use case

Quickly and accurately determining the most efficient route between a location and a facility is a frequently encountered task. For example, a city’s fire department may need to know which fire stations in the vicinity offer the quickest routes to multiple fires. Solving for the closest fire station to the fire’s location using an impedance of “travel time” would provide this information.

How to use the sample

Click the ‘Solve Routes’ button to solve and display the route from each incident (fire) to the nearest facility (fire station).

How it works

  1. Create a ClosestFacilityTask using a URL from an online service.
  2. Get the default set of ClosestFacilityParameters from the task: closestFacilityTask.createDefaultParametersAsync().get().
  3. Build a list of all Facilitys and Incidents:
  • Create a FeatureTable using ServiceFeatureTable(Uri).
  • Query the FeatureTable for all Features using queryFeaturesAsync(queryParameters).
  • Iterate over the result and add each Feature to the List, instantiating the feature as a Facility or Incident.
  1. Add a list of all facilities to the task parameters: closestFacilityParameters.setFacilities(facilitiesList).
  2. Add a list of all incidents to the task parameters: closestFacilityParameters.setIncidents(incidentsList).
  3. Get ClosestFacilityResult by solving the task with the provided parameters: closestFacilityTask.solveClosestFacilityAsync(closestFacilityParameters).
  4. Find the closest facility for each incident by iterating over the list of Incidents.
  5. Display the route as a Graphic using the closestFacilityRoute.getRouteGeometry().

Relevant API

  • ClosestFacilityParameters
  • ClosestFacilityResult
  • ClosestFacilityRoute
  • ClosestFacilityTask
  • Facility
  • Graphic
  • GraphicsOverlay
  • Incident

Tags

facility, incident, network analysis, route, search

Sample Code

module-info.java module-info.java ClosestFacilityStaticSample.java
/*
* Copyright 2022 Esri.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
module com.esri.samples.closest_facility_static {
// require ArcGIS Maps SDK for Java module
requires com.esri.arcgisruntime;
// handle SLF4J http://www.slf4j.org/codes.html#StaticLoggerBinder
requires org.slf4j.nop;
// require JavaFX modules that the application uses
requires javafx.graphics;
requires javafx.controls;
exports com.esri.samples.closest_facility_static;
}