Find a route to the closest facility from a location.

Use case
Quickly and accurately determining the most efficient route between a location and a facility is a frequently encountered task. For example, a paramedic may need to know which hospital in the vicinity offers the possibility of getting an ambulance patient critical medical care in the shortest amount of time. Solving for the closest hospital to the ambulance’s location using an impedance of “travel time” would provide this information.
How to use the sample
Tap near any of the hospitals and a route will be displayed from that tapped location to the nearest hospital.
How it works
- Create a
ClosestFacilityTaskusing an URL from an online network analysis service. - Get
ClosestFacilityParametersfrom task,closestFacilityTask.createDefaultParameters(). - Add the list of facilities to parameters,
closestFacilityParameters.setFacilities(facilitiesList). - Add the incident to parameters,
closestFacilityParameters.setIncidents(listOf(incidentPoint)). - Get
ClosestFacilityResultfrom solving task with parameters,closestFacilityTask.solveClosestFacility(closestFacilityParameters). - Get index list of closet facilities to incident,
closestFacilityResult.getRankedFacilityIndexes(incidentIndex). - Get index of closest facility,
rankedFacilitiesList[0]. - Find closest facility route,
closestFacilityResult.getRoute(closestFacilityIndex, incidentIndex). - Display route to
MapView:- Create
Graphicfrom route geometry,Graphic(geometry = route?.routeGeometry, symbol = routeSymbol). - Add graphic to a
GraphicsOverlayand add it to the MapView’s graphic overlays collection.
- Create
Relevant API
- ClosestFacilityParameters
- ClosestFacilityResult
- ClosestFacilityRoute
- ClosestFacilityTask
- Facility
- Graphic
- GraphicsOverlay
- Incident
- MapView
Additional information
This sample uses the GeoView-Compose Toolkit module to be able to implement a composable MapView.
Tags
geoview-compose, incident, network analysis, route, search
Sample Code
/* Copyright 2024 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. * */
package com.esri.arcgismaps.sample.findclosestfacilityfrompoint
import android.os.Bundleimport androidx.activity.ComponentActivityimport androidx.activity.compose.setContentimport androidx.activity.enableEdgeToEdgeimport androidx.compose.material3.MaterialThemeimport androidx.compose.material3.Surfaceimport androidx.compose.runtime.Composableimport com.arcgismaps.ApiKeyimport com.arcgismaps.ArcGISEnvironmentimport com.esri.arcgismaps.sample.sampleslib.theme.SampleAppThemeimport com.esri.arcgismaps.sample.findclosestfacilityfrompoint.screens.MainScreen
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // authentication with an API key or named user is // required to access basemaps and other location services ArcGISEnvironment.apiKey = ApiKey.create(BuildConfig.ACCESS_TOKEN)
enableEdgeToEdge() setContent { SampleAppTheme { SampleApp() } } }
@Composable private fun SampleApp() { Surface( color = MaterialTheme.colorScheme.background ) { MainScreen( sampleName = getString(R.string.find_closest_facility_from_point_app_name) ) } }}/* Copyright 2024 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. * */
package com.esri.arcgismaps.sample.findclosestfacilityfrompoint.components
import android.app.Applicationimport androidx.lifecycle.AndroidViewModelimport androidx.lifecycle.viewModelScopeimport com.arcgismaps.Colorimport com.arcgismaps.geometry.Pointimport com.arcgismaps.geometry.SpatialReferenceimport com.arcgismaps.mapping.ArcGISMapimport com.arcgismaps.mapping.BasemapStyleimport com.arcgismaps.mapping.Viewpointimport com.arcgismaps.mapping.symbology.PictureMarkerSymbolimport com.arcgismaps.mapping.symbology.SimpleLineSymbolimport com.arcgismaps.mapping.symbology.SimpleLineSymbolStyleimport com.arcgismaps.mapping.symbology.SimpleMarkerSymbolimport com.arcgismaps.mapping.symbology.SimpleMarkerSymbolStyleimport com.arcgismaps.mapping.view.Graphicimport com.arcgismaps.mapping.view.GraphicsOverlayimport com.arcgismaps.mapping.view.SingleTapConfirmedEventimport com.arcgismaps.tasks.networkanalysis.ClosestFacilityParametersimport com.arcgismaps.tasks.networkanalysis.ClosestFacilityResultimport com.arcgismaps.tasks.networkanalysis.ClosestFacilityTaskimport com.arcgismaps.tasks.networkanalysis.Facilityimport com.arcgismaps.tasks.networkanalysis.Incidentimport com.esri.arcgismaps.sample.findclosestfacilityfrompoint.Rimport com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModelimport kotlinx.coroutines.launch
class MapViewModel(private var application: Application) : AndroidViewModel(application) {
// List of hospital facilities in the San Diego area private var facilitiesList = listOf( Facility(Point(-1.3042129900625112E7, 3860127.9479775648, SpatialReference.webMercator())), Facility(Point(-1.3042193400557665E7, 3862448.873041752, SpatialReference.webMercator())), Facility(Point(-1.3046882875518233E7, 3862704.9896770366, SpatialReference.webMercator())), Facility(Point(-1.3040539754780494E7, 3862924.5938606677, SpatialReference.webMercator())), Facility(Point(-1.3042571225655518E7, 3858981.773018156, SpatialReference.webMercator())), Facility(Point(-1.3039784633928463E7, 3856692.5980474586, SpatialReference.webMercator())), Facility(Point(-1.3049023883956768E7, 3861993.789732541, SpatialReference.webMercator())) )
// Create graphic overlay of facilities to display on the map private val facilityGraphicsOverlay = GraphicsOverlay()
// Create graphic overlay of incident to display on map tapped private val incidentGraphicsOverlay = GraphicsOverlay()
// Create a ViewModel to handle dialog interactions private val messageDialogVM: MessageDialogViewModel = MessageDialogViewModel()
// Add the graphics overlays to be used by the composable MapView val graphicsOverlays = listOf(facilityGraphicsOverlay, incidentGraphicsOverlay)
// Create a streets basemap layer and change the position of map to center around San Diego val map = ArcGISMap(BasemapStyle.ArcGISStreets).apply { initialViewpoint = Viewpoint( latitude = 32.727, longitude = -117.1750, scale = 100000.0 ) }
init { // Create the facility symbol val facilityUrl = application.getString(R.string.hospital_symbol_url) val facilitySymbol = PictureMarkerSymbol(facilityUrl).apply { height = 30F width = 30F }
// Create a graphic for each facility and add them to the graphics overlay for (facility in facilitiesList) { val facilityGraphic = Graphic( geometry = facility.geometry, symbol = facilitySymbol ) facilityGraphicsOverlay.graphics.add(facilityGraphic) } }
/** * Retrieve the tapped point [event], update the incident, and initiate the process * to find the route between the incident and closest facility. */ fun onSingleTapConfirmed(event: SingleTapConfirmedEvent) {
// Retrieve the tapped map point from the SingleTapConfirmedEvent val mapPoint: Point = event.mapPoint ?: return messageDialogVM.showMessageDialog( title = "No map point retrieved from tap." )
// Create an incident on the tapped point val incidentPoint = Incident(mapPoint)
// Clear and update the incident graphic on the map updateIncidentGraphicPosition(incidentPoint)
// Find the closest facility to the incident viewModelScope.launch { findRoute(incidentPoint) } }
/** * Updates the [incidentGraphicsOverlay] to display a graphic on the [incidentPoint] */ private fun updateIncidentGraphicPosition(incidentPoint: Incident) {
incidentGraphicsOverlay.graphics.clear()
// Create a cross symbol for the incident val incidentMarker = SimpleMarkerSymbol( style = SimpleMarkerSymbolStyle.Cross, color = Color.black, size = 20.0F )
// Get the incident's geometry val incidentStopGeometry = incidentPoint.geometry
// Add graphic to incident graphic overlays incidentGraphicsOverlay.graphics.add( Graphic( geometry = incidentStopGeometry, symbol = incidentMarker ) )
}
/** * Find the route between the given [incidentPoint] and the closest available facility * */ private suspend fun findRoute(incidentPoint: Incident) { val closestFacilityTask = ClosestFacilityTask( url = application.getString(R.string.san_diego_network_service_url) )
// Create a job to find the route val closestFacilityParameters: ClosestFacilityParameters = closestFacilityTask.createDefaultParameters().getOrThrow() // Add the facilities and incident points closestFacilityParameters.apply { setFacilities(facilitiesList) setIncidents(listOf(incidentPoint)) }
// Solve a route using the facility task with the created parameters. val closestFacilityResult = closestFacilityTask.solveClosestFacility(closestFacilityParameters).getOrElse { error -> messageDialogVM.showMessageDialog( title = "Error solving route: ${error.message.toString()}", description = error.cause.toString() ) } as ClosestFacilityResult val rankedFacilitiesList = closestFacilityResult.getRankedFacilityIndexes(incidentIndex = 0)
// If the result contains a facility, solve the route from incident to facility. if (rankedFacilitiesList.isNotEmpty()) { val closestFacilityIndex = rankedFacilitiesList[0] val route = closestFacilityResult.getRoute( facilityIndex = closestFacilityIndex, incidentIndex = 0 ) val routeSymbol = SimpleLineSymbol( style = SimpleLineSymbolStyle.Solid, color = Color.fromRgba(0, 0, 255), width = 2.0f ) val routeGraphic = Graphic( geometry = route?.routeGeometry, symbol = routeSymbol )
// Add the route graphic to the incident graphic overlay incidentGraphicsOverlay.graphics.add(routeGraphic) }
}}/* Copyright 2024 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. * */
package com.esri.arcgismaps.sample.findclosestfacilityfrompoint.screens
import androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.paddingimport androidx.compose.material3.Scaffoldimport androidx.compose.runtime.Composableimport androidx.compose.ui.Modifierimport androidx.lifecycle.viewmodel.compose.viewModelimport com.arcgismaps.toolkit.geoviewcompose.MapViewimport com.esri.arcgismaps.sample.findclosestfacilityfrompoint.components.MapViewModelimport com.esri.arcgismaps.sample.sampleslib.components.SampleTopAppBar
/** * Main screen layout for the sample app */@Composablefun MainScreen(sampleName: String) {
val mapViewModel: MapViewModel = viewModel()
Scaffold( topBar = { SampleTopAppBar(title = sampleName) }, content = { MapView( modifier = Modifier .fillMaxSize() .padding(it), arcGISMap = mapViewModel.map, graphicsOverlays = mapViewModel.graphicsOverlays, onSingleTapConfirmed = { event -> mapViewModel.onSingleTapConfirmed(event) }, ) } )}