Find service area (interactive)

View on GitHubSample viewer app

Find the service area within a network from a given point.

Image of find service area

Use case

A service area shows locations that can be reached from a facility based off a certain impedance, such as travel time or distance. Barriers can increase impedance by either adding to the time it takes to pass through the barrier or by altogether preventing passage.

You might calculate the region around a hospital in which ambulances can service in 30 min or less.

How to use the sample

In order to find any service areas at least one facility needs to be added to the map view.

  • To add a facility, tap the facility button, then tap anywhere on the map.
  • To add a barrier, tap the barrier button, and tap multiple locations on map. Hit the barrier button again to finish drawing barrier. Hitting any other button will also stop the barrier from drawing.
  • To show service areas around facilities that were added, tap show service areas button.
  • The reset button clears all graphics and resets the service area task.

How it works

  1. Create a new ServiceAreaTask from a network service.
  2. Create default ServiceAreaParameters from the service area task.
  3. Set the parameters to return polygons (true) to return all service areas.
  4. Add a ServiceAreaFacility to the parameters.
  5. Get the ServiceAreaResult by solving the service area task using the parameters.
  6. Get any ServiceAreaPolygons that were returned, serviceAreaResult.getResultPolygons(facilityIndex).
  7. Display the service area polygons as graphics in a GraphicsOverlay on the MapView.

Relevant API

  • PolylineBarrier
  • ServiceAreaFacility
  • ServiceAreaParameters
  • ServiceAreaPolygon
  • ServiceAreaResult
  • ServiceAreaTask

Tags

barriers, facilities, impedance, logistics, routing

Sample Code

MainActivity.java
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 *  Copyright 2019 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.arcgisruntime.sample.findserviceareainteractive;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.Polyline;
import com.esri.arcgisruntime.geometry.PolylineBuilder;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.PictureMarkerSymbol;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.tasks.networkanalysis.PolylineBarrier;
import com.esri.arcgisruntime.tasks.networkanalysis.ServiceAreaFacility;
import com.esri.arcgisruntime.tasks.networkanalysis.ServiceAreaParameters;
import com.esri.arcgisruntime.tasks.networkanalysis.ServiceAreaPolygon;
import com.esri.arcgisruntime.tasks.networkanalysis.ServiceAreaPolygonDetail;
import com.esri.arcgisruntime.tasks.networkanalysis.ServiceAreaResult;
import com.esri.arcgisruntime.tasks.networkanalysis.ServiceAreaTask;

public class MainActivity extends AppCompatActivity {

  private static final String TAG = MainActivity.class.getSimpleName();

  private MapView mMapView;
  private ServiceAreaParameters mServiceAreaParameters;
  private PolylineBuilder mBarrierBuilder;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // authentication with an API key or named user is required to access basemaps and other
    // location services
    ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY);

    // get a reference to the map view
    mMapView = findViewById(R.id.mapView);

    // create a map with a streets base map
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_STREETS);
    mMapView.setMap(map);

    // set the view point to San Diego, where the service task area is
    mMapView.setViewpoint(new Viewpoint(32.73, -117.14, 75000));

    // create service area task from url
    ServiceAreaTask serviceAreaTask = new ServiceAreaTask(this, getString(R.string.san_diego_service_area));
    serviceAreaTask.loadAsync();
    // create default parameters from task
    ListenableFuture<ServiceAreaParameters> serviceAreaParametersFuture = serviceAreaTask
        .createDefaultParametersAsync();
    serviceAreaParametersFuture.addDoneListener(() -> {
      try {
        mServiceAreaParameters = serviceAreaParametersFuture.get();
        mServiceAreaParameters.setPolygonDetail(ServiceAreaPolygonDetail.HIGH);
        mServiceAreaParameters.setReturnPolygons(true);
        // adding another service area of 2 minutes
        // default parameters have a default service area of 5 minutes
        mServiceAreaParameters.getDefaultImpedanceCutoffs().addAll(Collections.singletonList(2.0));
      } catch (ExecutionException | InterruptedException e) {
        String error = "Error creating service area parameters: " + e;
        Log.e(TAG, error);
        Toast.makeText(this, error, Toast.LENGTH_LONG).show();
      }
    });

    // create graphics overlays to show facilities, barriers and service areas
    GraphicsOverlay serviceAreasOverlay = new GraphicsOverlay();
    GraphicsOverlay facilityOverlay = new GraphicsOverlay();
    GraphicsOverlay barrierOverlay = new GraphicsOverlay();
    mMapView.getGraphicsOverlays().addAll(Arrays.asList(serviceAreasOverlay, barrierOverlay, facilityOverlay));

    mBarrierBuilder = new PolylineBuilder(mMapView.getSpatialReference());
    List<ServiceAreaFacility> serviceAreaFacilities = new ArrayList<>();

    SimpleLineSymbol barrierLine = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.BLACK, 3.0f);
    ArrayList<SimpleFillSymbol> fillSymbols = new ArrayList<>();
    fillSymbols.add(new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, getColor(R.color.colorTransparentRed), null));
    fillSymbols.add(new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, getColor(R.color.colorTransparentOrange), null));

    // icon used to display facilities to map view
    PictureMarkerSymbol facilitySymbol = new PictureMarkerSymbol(getString(R.string.hospital_icon_url));
    facilitySymbol.setHeight(30);
    facilitySymbol.setWidth(30);

    Button addFacilityButton = findViewById(R.id.addFacilityButton);
    Button addBarrierButton = findViewById(R.id.addBarrierButton);
    addFacilityButton.setOnClickListener(v -> {
      addFacilityButton.setSelected(true);
      addBarrierButton.setSelected(false);
    });
    addBarrierButton.setOnClickListener(v -> {
      addBarrierButton.setSelected(true);
      addFacilityButton.setSelected(false);
      mBarrierBuilder = new PolylineBuilder(mMapView.getSpatialReference());
    });

    Button showServiceAreasButton = findViewById(R.id.showServiceAreasButton);
    showServiceAreasButton.setOnClickListener(
        v -> showServiceAreas(serviceAreaFacilities, barrierOverlay, serviceAreasOverlay, serviceAreaTask, fillSymbols,
            addFacilityButton, addBarrierButton));

    Button resetButton = findViewById(R.id.resetButton);
    resetButton.setOnClickListener(
        v -> clearRouteAndGraphics(addFacilityButton, addBarrierButton, serviceAreaFacilities, facilityOverlay, serviceAreasOverlay, barrierOverlay));

    // creates facilities and barriers at user's clicked location
    mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
      @Override public boolean onSingleTapConfirmed(MotionEvent e) {
        // create a point where the user tapped
        android.graphics.Point point = new android.graphics.Point(Math.round(e.getX()), Math.round(e.getY()));
        Point mapPoint = mMapView.screenToLocation(point);
        if (addFacilityButton.isSelected()) {
          // create facility from point and display to map view
          addServicePoint(mapPoint, facilitySymbol, serviceAreaFacilities, facilityOverlay);
        } else if (addBarrierButton.isSelected()) {
          // create barrier and display to map view
          mBarrierBuilder.addPoint(new Point(mapPoint.getX(), mapPoint.getY(), mMapView.getSpatialReference()));
          barrierOverlay.getGraphics()
              .add(barrierOverlay.getGraphics().size(), new Graphic(mBarrierBuilder.toGeometry(), barrierLine));
        }
        return super.onSingleTapConfirmed(e);
      }
    });
  }

  /**
   * Add the given point to the list of service areas and use it to create a facility graphic, which is then added to
   * the facility overlay.
   */
  private void addServicePoint(Point mapPoint, PictureMarkerSymbol facilitySymbol,
      List<ServiceAreaFacility> serviceAreaFacilities, GraphicsOverlay facilityOverlay) {
    Point servicePoint = new Point(mapPoint.getX(), mapPoint.getY(), mMapView.getSpatialReference());
    serviceAreaFacilities.add(new ServiceAreaFacility(servicePoint));
    facilityOverlay.getGraphics().add(new Graphic(servicePoint, facilitySymbol));
  }

  /**
   * Clears all graphics from map view and clears all facilities and barriers from service area parameters.
   */
  private void clearRouteAndGraphics(Button addFacilityButton, Button addBarrierButton,
      List<ServiceAreaFacility> serviceAreaFacilities, GraphicsOverlay facilityOverlay,
      GraphicsOverlay serviceAreasOverlay, GraphicsOverlay barrierOverlay) {
    addFacilityButton.setSelected(false);
    addBarrierButton.setSelected(false);
    mServiceAreaParameters.clearFacilities();
    mServiceAreaParameters.clearPolylineBarriers();
    serviceAreaFacilities.clear();
    facilityOverlay.getGraphics().clear();
    serviceAreasOverlay.getGraphics().clear();
    barrierOverlay.getGraphics().clear();
  }

  /**
   * Solves the service area task using the facilities and barriers that were added to the map view.
   * All service areas that are return will be displayed to the map view.
   */
  private void showServiceAreas(List<ServiceAreaFacility> serviceAreaFacilities, GraphicsOverlay barrierOverlay,
      GraphicsOverlay serviceAreasOverlay, ServiceAreaTask serviceAreaTask, ArrayList<SimpleFillSymbol> fillSymbols,
      Button addFacilityButton, Button addBarrierButton) {

    // need at least one facility for the task to work
    if (!serviceAreaFacilities.isEmpty()) {
      // un-select add facility and add barrier buttons
      addFacilityButton.setSelected(false);
      addBarrierButton.setSelected(false);
      List<PolylineBarrier> polylineBarriers = new ArrayList<>();
      for (Graphic barrierGraphic : barrierOverlay.getGraphics()) {
        polylineBarriers.add(new PolylineBarrier((Polyline) barrierGraphic.getGeometry()));
        mServiceAreaParameters.setPolylineBarriers(polylineBarriers);
      }

      serviceAreasOverlay.getGraphics().clear();
      mServiceAreaParameters.setFacilities(serviceAreaFacilities);
      // find service areas around facility using parameters that were set
      ListenableFuture<ServiceAreaResult> result = serviceAreaTask.solveServiceAreaAsync(mServiceAreaParameters);
      result.addDoneListener(() -> {
        try {
          // display all service areas that were found to mapview
          List<Graphic> graphics = serviceAreasOverlay.getGraphics();
          ServiceAreaResult serviceAreaResult = result.get();
          for (int i = 0; i < serviceAreaFacilities.size(); i++) {
            List<ServiceAreaPolygon> polygons = serviceAreaResult.getResultPolygons(i);
            // could be more than one service area
            for (int j = 0; j < polygons.size(); j++) {
              graphics.add(new Graphic(polygons.get(j).getGeometry(), fillSymbols.get(j % 2)));
            }
          }
        } catch (ExecutionException | InterruptedException e) {
          String error = e.getMessage().contains("Unable to complete operation") ?
              "Facility not within San Diego area!" + e :
              "Error getting the service area result: " + e;
          Log.e(TAG, error);
          Toast.makeText(this, error, Toast.LENGTH_LONG).show();
        }
      });
    } else {
      Toast.makeText(this, "Must have at least one Facility on the map!", Toast.LENGTH_LONG).show();
    }
  }

  @Override
  protected void onPause() {
    mMapView.pause();
    super.onPause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    mMapView.resume();
  }

  @Override
  protected void onDestroy() {
    mMapView.dispose();
    super.onDestroy();
  }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.