|
| 1 | +// Copyright 2023 Esri |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import ArcGIS |
| 16 | +import SwiftUI |
| 17 | + |
| 18 | +struct FindAddressWithReverseGeocodeView: View { |
| 19 | + /// The view model for the sample. |
| 20 | + @StateObject private var model = Model() |
| 21 | + |
| 22 | + /// The point on the map where the user tapped. |
| 23 | + @State private var tapLocation: Point? |
| 24 | + |
| 25 | + /// The placement of the callout on the map. |
| 26 | + @State private var calloutPlacement: CalloutPlacement? |
| 27 | + |
| 28 | + /// The text shown in the callout. |
| 29 | + @State private var calloutText: String? |
| 30 | + |
| 31 | + /// The error shown in the error alert. |
| 32 | + @State private var error: Error? |
| 33 | + |
| 34 | + var body: some View { |
| 35 | + MapView(map: model.map, graphicsOverlays: [model.graphicsOverlay]) |
| 36 | + .callout(placement: $calloutPlacement.animation(.default.speed(2))) { _ in |
| 37 | + Text(calloutText ?? "No address found.") |
| 38 | + .font(.callout) |
| 39 | + .padding(8) |
| 40 | + } |
| 41 | + .onSingleTapGesture { _, mapPoint in |
| 42 | + tapLocation = mapPoint |
| 43 | + } |
| 44 | + .task(id: tapLocation) { |
| 45 | + guard let tapLocation else { return } |
| 46 | + await reverseGeocode(tapLocation) |
| 47 | + } |
| 48 | + .task { |
| 49 | + do { |
| 50 | + try await model.locatorTask.load() |
| 51 | + } catch { |
| 52 | + self.error = error |
| 53 | + } |
| 54 | + } |
| 55 | + .overlay(alignment: .top) { |
| 56 | + Text("Tap on the map to get the address for point.") |
| 57 | + .frame(maxWidth: .infinity, alignment: .center) |
| 58 | + .padding(8) |
| 59 | + .background(.thinMaterial, ignoresSafeAreaEdges: .horizontal) |
| 60 | + } |
| 61 | + .errorAlert(presentingError: $error) |
| 62 | + } |
| 63 | + |
| 64 | + /// Reverse geocodes a given point and updates the marker with the result. |
| 65 | + /// - Parameter mapPoint: The point on the map to reverse geocode. |
| 66 | + private func reverseGeocode(_ mapPoint: Point) async { |
| 67 | + // Normalize the map point. |
| 68 | + guard let normalizedPoint = GeometryEngine.normalizeCentralMeridian( |
| 69 | + of: mapPoint |
| 70 | + ) as? Point else { return } |
| 71 | + |
| 72 | + do { |
| 73 | + // Perform reverse geocode using the locator task with the point and parameters. |
| 74 | + let geocodeResults = try await model.locatorTask.reverseGeocode( |
| 75 | + forLocation: normalizedPoint, |
| 76 | + parameters: model.reverseGeocodeParameters |
| 77 | + ) |
| 78 | + |
| 79 | + // Update the callout text using the first result from the reverse geocode. |
| 80 | + updateCalloutText(using: geocodeResults.first) |
| 81 | + } catch { |
| 82 | + self.error = error |
| 83 | + calloutText = nil |
| 84 | + } |
| 85 | + |
| 86 | + // Update the marker and callout location. |
| 87 | + model.markerGraphic.geometry = normalizedPoint |
| 88 | + calloutPlacement = .geoElement(model.markerGraphic, tapLocation: normalizedPoint) |
| 89 | + } |
| 90 | + |
| 91 | + /// Updates the callout text using the address from a given geocode result. |
| 92 | + /// - Parameter geocodeResult: The result to get the address from. |
| 93 | + private func updateCalloutText(using geocodeResult: GeocodeResult?) { |
| 94 | + // Get the address from the result's attributes. |
| 95 | + let addressText = model.reverseGeocodeParameters.resultAttributeNames |
| 96 | + .compactMap { geocodeResult?.attributes[$0] as? String } |
| 97 | + .filter { !$0.isEmpty } |
| 98 | + .joined(separator: ", ") |
| 99 | + |
| 100 | + // Update the callout text if an address was found. |
| 101 | + calloutText = !addressText.isEmpty ? addressText : nil |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +private extension FindAddressWithReverseGeocodeView { |
| 106 | + /// The model used to store the geo model and other expensive objects used in this view. |
| 107 | + class Model: ObservableObject { |
| 108 | + /// A map with a topographic basemap initially centered on Redlands, CA, USA. |
| 109 | + let map = { |
| 110 | + let map = Map(basemapStyle: .arcGISTopographic) |
| 111 | + map.initialViewpoint = Viewpoint( |
| 112 | + center: Point(x: -117.195, y: 34.058, spatialReference: .wgs84), |
| 113 | + scale: 5e4 |
| 114 | + ) |
| 115 | + return map |
| 116 | + }() |
| 117 | + |
| 118 | + /// The graphics overlay for the marker graphic. |
| 119 | + let graphicsOverlay = GraphicsOverlay() |
| 120 | + |
| 121 | + /// The red map marker graphic used to indicate a tap location on the map. |
| 122 | + let markerGraphic = { |
| 123 | + // Create a symbol using the image from the project assets. |
| 124 | + guard let markerImage = UIImage(named: "RedMarker") else { return Graphic() } |
| 125 | + let markerSymbol = PictureMarkerSymbol(image: markerImage) |
| 126 | + |
| 127 | + // Change the symbol's offsets, so it aligns properly to a given point. |
| 128 | + markerSymbol.leaderOffsetY = markerImage.size.height / 2 |
| 129 | + markerSymbol.offsetY = markerImage.size.height / 2 |
| 130 | + |
| 131 | + // Create a graphic with the symbol. |
| 132 | + return Graphic(symbol: markerSymbol) |
| 133 | + }() |
| 134 | + |
| 135 | + /// The locator task for reverse geocoding. |
| 136 | + let locatorTask = LocatorTask(url: .geocodeServer) |
| 137 | + |
| 138 | + /// The parameters for the reverse geocode operation. |
| 139 | + let reverseGeocodeParameters = { |
| 140 | + let parameters = ReverseGeocodeParameters() |
| 141 | + parameters.addResultAttributeNames(["Address", "City", "RegionAbbr"]) |
| 142 | + parameters.maxResults = 1 |
| 143 | + return parameters |
| 144 | + }() |
| 145 | + |
| 146 | + init() { |
| 147 | + graphicsOverlay.addGraphic(markerGraphic) |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +private extension URL { |
| 153 | + /// A URL to a geocode server on ArcGIS Online. |
| 154 | + static var geocodeServer: URL { |
| 155 | + URL(string: "https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer")! |
| 156 | + } |
| 157 | +} |
0 commit comments