Find service area interactive

View on GitHubSample viewer app

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

Find service area interactive sample

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. Tap the barrier button again to finish drawing barrier. Tapping 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 AGSServiceAreaTask from a network service.
  2. Create default AGSServiceAreaParameters from the service area task.
  3. Set the AGSPolygonBarrier to polygon.
  4. Add a AGSServiceAreaFacility to the parameters.
  5. Get the AGSServiceAreaResult by solving the service area task using the parameters.
  6. Get any AGSServiceAreaPolygons that were returned by using AGSServiceAreaResult.resultPolygons(atFacilityIndex:).
  7. Display the service area polygons as graphics in an AGSGraphicsOverlay on the mapView.

Relevant API

  • AGSPolygonBarrier
  • AGSServiceAreaFacility
  • AGSServiceAreaParameters
  • AGSServiceAreaPolygon
  • AGSServiceAreaResult
  • AGSServiceAreaTask

Tags

barriers, facilities, impedance, logistics, routing

Sample Code

FindServiceAreaInteractiveViewController.swiftFindServiceAreaInteractiveViewController.swiftServiceAreaSettingsViewController.swift
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
//
// Copyright 2017 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.

import UIKit
import ArcGIS

class FindServiceAreaInteractiveViewController: UIViewController, AGSGeoViewTouchDelegate, ServiceAreaSettingsViewControllerDelegate, UIAdaptivePresentationControllerDelegate {
    @IBOutlet private var mapView: AGSMapView!
    @IBOutlet private var segmentedControl: UISegmentedControl!
    @IBOutlet private var serviceAreaBBI: UIBarButtonItem!

    private var facilitiesGraphicsOverlay = AGSGraphicsOverlay()
    private var barriersGraphicsOverlay = AGSGraphicsOverlay()
    private var serviceAreaGraphicsOverlay = AGSGraphicsOverlay()
    private var barrierGraphic: AGSGraphic!
    private var serviceAreaTask: AGSServiceAreaTask!
    private var serviceAreaParameters: AGSServiceAreaParameters!

    var firstTimeBreak: Int = 3
    var secondTimeBreak: Int = 8

    override func viewDidLoad() {
        super.viewDidLoad()

        // add the source code button item to the right of navigation bar
        (self.navigationItem.rightBarButtonItem as! SourceCodeBarButtonItem).filenames = ["FindServiceAreaInteractiveViewController", "ServiceAreaSettingsViewController"]

        // initialize map with basemap
        let map = AGSMap(basemapStyle: .arcGISTerrain)

        // center for initial viewpoint
        let center = AGSPoint(x: -13041154, y: 3858170, spatialReference: .webMercator())

        // assign map to map view
        self.mapView.map = map
        self.mapView.setViewpoint(AGSViewpoint(center: center, scale: 1e5))

        // assign touch delegate as self to know when use interacted with the map view
        // Will be adding facilities and barriers on interaction
        self.mapView.touchDelegate = self

        // initialize service area task
        self.serviceAreaTask = AGSServiceAreaTask(url: URL(string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/NetworkAnalysis/SanDiego/NAServer/ServiceArea")!)

        // get default parameters for the task
        self.getDefaultParameters()

        // facility picture marker symbol
        let facilitySymbol = AGSPictureMarkerSymbol(image: UIImage(named: "Facility")!)

        // offset symbol in Y to align image properly
        facilitySymbol.offsetY = 21

        // assign renderer on facilities graphics overlay using the picture marker symbol
        self.facilitiesGraphicsOverlay.renderer = AGSSimpleRenderer(symbol: facilitySymbol)

        // barrier symbol
        let barrierSymbol = AGSSimpleFillSymbol(style: .diagonalCross, color: .red, outline: nil)

        // set symbol on barrier graphics overlay using renderer
        self.barriersGraphicsOverlay.renderer = AGSSimpleRenderer(symbol: barrierSymbol)

        // add graphicOverlays to the map. One for facilities, barriers and service areas
        self.mapView.graphicsOverlays.addObjects(from: [self.serviceAreaGraphicsOverlay, self.barriersGraphicsOverlay, self.facilitiesGraphicsOverlay])
    }

    private func getDefaultParameters() {
        // get default parameters
        self.serviceAreaTask.defaultServiceAreaParameters { [weak self] (parameters: AGSServiceAreaParameters?, error: Error?) in
            guard error == nil else {
                self?.presentAlert(message: "Error getting default parameters:: \(error!.localizedDescription)")
                return
            }

            // keep a reference to the default parameters to be used later
            self?.serviceAreaParameters = parameters

            // enable service area bar button item
            self?.serviceAreaBBI.isEnabled = true
        }
    }

    private func serviceAreaSymbol(for index: Int) -> AGSSymbol {
        // fill symbol for service area
        var fillSymbol: AGSSimpleFillSymbol

        if index == 0 {
            let lineSymbol = AGSSimpleLineSymbol(style: .solid, color: UIColor(red: 0.4, green: 0.4, blue: 0, alpha: 0.3), width: 2)
            fillSymbol = AGSSimpleFillSymbol(style: .solid, color: UIColor(red: 0.8, green: 0.8, blue: 0, alpha: 0.3), outline: lineSymbol)
        } else {
            let lineSymbol = AGSSimpleLineSymbol(style: .solid, color: UIColor(red: 0, green: 0.4, blue: 0, alpha: 0.3), width: 2)
            fillSymbol = AGSSimpleFillSymbol(style: .solid, color: UIColor(red: 0, green: 0.8, blue: 0, alpha: 0.3), outline: lineSymbol)
        }

        return fillSymbol
    }

    // MARK: - Actions

    @IBAction private func serviceArea() {
        // remove previously added service areas
        serviceAreaGraphicsOverlay.graphics.removeAllObjects()

        let facilitiesGraphics = facilitiesGraphicsOverlay.graphics as! [AGSGraphic]

        // check if at least a single facility is added
        guard !facilitiesGraphics.isEmpty else {
            presentAlert(message: "At least one facility is required")
            return
        }

        // add facilities
        var facilities = [AGSServiceAreaFacility]()

        // for each graphic in facilities graphicsOverlay add a facility to the parameters
        for graphic in facilitiesGraphics {
            let point = graphic.geometry as! AGSPoint
            let facility = AGSServiceAreaFacility(point: point)
            facilities.append(facility)
        }
        self.serviceAreaParameters.setFacilities(facilities)

        // add barriers
        var barriers = [AGSPolygonBarrier]()

        // for each graphic in barrier graphicsOverlay add a barrier to the parameters
        for graphic in barriersGraphicsOverlay.graphics as! [AGSGraphic] {
            let polygon = graphic.geometry as! AGSPolygon
            let barrier = AGSPolygonBarrier(polygon: polygon)
            barriers.append(barrier)
        }
        serviceAreaParameters.setPolygonBarriers(barriers)

        // set time breaks
        serviceAreaParameters.defaultImpedanceCutoffs = [NSNumber(value: firstTimeBreak), NSNumber(value: secondTimeBreak)]

        serviceAreaParameters.geometryAtOverlap = .dissolve

        // show progress hud
        UIApplication.shared.showProgressHUD(message: "Loading")

        // solve for service area
        serviceAreaTask.solveServiceArea(with: serviceAreaParameters) { [weak self] (result: AGSServiceAreaResult?, error: Error?) in
            // dismiss progress hud
            UIApplication.shared.hideProgressHUD()

            guard let self = self else {
                return
            }

            if let error = error {
                self.presentAlert(message: "Error solving service area: \(error.localizedDescription)")
            } else {
                // add resulting polygons as graphics to the overlay
                // since we are using `geometryAtOVerlap` as `dissolve` and the cutoff values
                // are the same across facilities, we only need to draw the resultPolygons at
                // facility index 0. It will contain either merged or multipart polygons
                if let polygons = result?.resultPolygons(atFacilityIndex: 0) {
                    for index in polygons.indices {
                        let polygon = polygons[index]
                        let fillSymbol = self.serviceAreaSymbol(for: index)
                        let graphic = AGSGraphic(geometry: polygon.geometry, symbol: fillSymbol, attributes: nil)
                        self.serviceAreaGraphicsOverlay.graphics.add(graphic)
                    }
                }
            }
        }
    }

    @IBAction private func clearAction() {
        // remove all existing graphics in service area and facilities graphics overlays
        self.serviceAreaGraphicsOverlay.graphics.removeAllObjects()
        self.facilitiesGraphicsOverlay.graphics.removeAllObjects()
        self.barriersGraphicsOverlay.graphics.removeAllObjects()
    }

    // MARK: - AGSGeoViewTouchDelegate

    func geoView(_ geoView: AGSGeoView, didTapAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
        if segmentedControl.selectedSegmentIndex == 0 {
            // facilities selected
            let graphic = AGSGraphic(geometry: mapPoint, symbol: nil, attributes: nil)
            self.facilitiesGraphicsOverlay.graphics.add(graphic)
        } else {
            // barriers selected
            let bufferedGeometry = AGSGeometryEngine.bufferGeometry(mapPoint, byDistance: 500)
            let graphic = AGSGraphic(geometry: bufferedGeometry, symbol: nil, attributes: nil)
            self.barriersGraphicsOverlay.graphics.add(graphic)
        }
    }

    // MARK: - Navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "ServiceAreaSettingsSegue" {
            let controller = segue.destination as! ServiceAreaSettingsViewController
            controller.presentationController?.delegate = self
            controller.preferredContentSize = CGSize(width: 300, height: 200)
            controller.delegate = self
            controller.firstTimeBreak = self.firstTimeBreak
            controller.secondTimeBreak = self.secondTimeBreak
        }
    }

    // MARK: - ServiceAreaSettingsViewControllerDelegate

    func serviceAreaSettingsViewController(_ serviceAreaSettingsViewController: ServiceAreaSettingsViewController, didUpdateFirstTimeBreak timeBreak: Int) {
        self.firstTimeBreak = timeBreak
    }

    func serviceAreaSettingsViewController(_ serviceAreaSettingsViewController: ServiceAreaSettingsViewController, didUpdateSecondTimeBreak timeBreak: Int) {
        self.secondTimeBreak = timeBreak
    }

    // MARK: - UIAdaptivePresentationControllerDelegate

    func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        return .none
    }
}

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