Search for an address

Learn how to find an address or place with a search bar and the geocoding service.

Overview of how to search for an address

Geocoding is the process of converting address or place text into a location. The geocoding service can search for an address or a place and perform reverse geocoding.

In this tutorial, you use a search bar in the user interface to access the Geocoding service and search for addresses and places.

Prerequisites

Before starting this tutorial:

  1. You need an ArcGIS Location Platform or ArcGIS Online account.

  2. Your system meets the system requirements.

Set up authentication

To access the secure ArcGIS location services used in this tutorial, you must implement API key authentication using an ArcGIS Location Platform or an ArcGIS Online account.

Create a new API key access token with privileges to access the secure resources used in this tutorial.

  1. Complete the Create an API key tutorial and create an API key with the following privilege(s):

    • Privileges
      • Location services > Basemaps
      • Location services > Geocoding
  2. Copy and paste the API Key access token into a safe location. It will be used in a later step.

Develop or Download

To complete this tutorial you have 2 options:

  1. Option 1: Develop the code or
  2. Option 2: Download the completed solution

Option 1: Develop the code

To start the tutorial, complete the Display a map tutorial. This creates a map to display the Santa Monica Mountains in California using the topographic basemap from the ArcGIS basemap styles service.

Continue with the following instructions to add a search bar to the user interface and search for an address or place using the ArcGIS geocoding service. First, you need to set the develop credentials in your app so that your app users can access the ArcGIS geocoding service.

Set developer credentials

To allow your app users to access ArcGIS location services, pass the developer credentials that you created in the Set up authentication step to the application's ArcGISEnvironment.

Pass your API Key access token to the ArcGISEnvironment.

  1. In the Project Navigator, click MainApp.swift.

  2. Set the ArcGISEnvironment.apiKey property with your API key access token.

    MainApp.swift
    Use dark colors for code blocks
    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
            ArcGISEnvironment.apiKey = APIKey("<#YOUR-ACCESS-TOKEN#>")
    

Best Practice: The access token is stored directly in the code as a convenience for this tutorial. In a production environment we do not recommend that you store it directly in source code.

Update the map

  1. In Xcode, in the Project Navigator, click ContentView.swift.

  2. In the editor, change the map and viewpoint into two separate @State variables so that the viewpoint can be changed as the geocode results change.

    ContentView.swift
    Use dark colors for code blocks
    55 56 57 58 59 60 61 62 63
    Change lineChange lineChange lineChange lineChange lineChange line
    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
    struct ContentView: View {
    
        @State private var map = Map(basemapStyle: .arcGISImagery)
        @State private var viewpoint: Viewpoint? = Viewpoint(
            latitude: 34.02700,
            longitude: -118.80500,
            scale: 72_000
        )
    
    
  3. Create a private class named Model of type ObservableObject and add a @StateObject variable of the Model to the ContentView. See the programming patterns page for more information on how to manage states.

    ContentView.swift
    Use dark colors for code blocks
    16 17 18 19 20 21 22 23 24 25 26 27 28
    Add line.Add line.Add line.Add line.
    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
    import SwiftUI
    
    import ArcGIS
    
    private class Model: ObservableObject {
    
    }
    
    struct ContentView: View {
    
        @StateObject private var model = Model()
    
    }
  4. Create a GraphicsOverlay named graphicsOverlay in the Model class. A graphics overlay is a container for graphics.

    ContentView.swift
    Use dark colors for code blocks
    20 21 22 23 24
    Add line.
    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
    private class Model: ObservableObject {
    
        let graphicsOverlay: GraphicsOverlay
    
    }
    
  5. Lastly, add the viewpoint and graphics overlay to the map view.

    ContentView.swift
    Use dark colors for code blocks
    88 89 90 91 92
    Change line
    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
        var body: some View {
    
            MapView(map: map, viewpoint: viewpoint, graphicsOverlays: [model.graphicsOverlay])
    
        }
    

Add graphics

Graphics are added as a visual means to display the search result on the map.

  1. Create a private property named textGraphic in the Model class. This graphic will be used to display the result's text label.

    ContentView.swift
    Expand
    Use dark colors for code blocks
    20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
    private class Model: ObservableObject {
    
        let graphicsOverlay: GraphicsOverlay
    
        let textGraphic: Graphic = {
            let textSymbol = TextSymbol(
                text: "",
                color: .black,
                size: 14,
                horizontalAlignment: .center,
                verticalAlignment: .bottom
            )
            textSymbol.backgroundColor = .white
            return Graphic(symbol: textSymbol)
        }()
    
    }
    
    Expand
  2. Create a private property named markerGraphic in the Model class. This graphic will be used to display the result's location.

    ContentView.swift
    Expand
    Use dark colors for code blocks
    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
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
    private class Model: ObservableObject {
    
        let graphicsOverlay: GraphicsOverlay
    
        let textGraphic: Graphic = {
            let textSymbol = TextSymbol(
                text: "",
                color: .black,
                size: 14,
                horizontalAlignment: .center,
                verticalAlignment: .bottom
            )
            textSymbol.backgroundColor = .white
            return Graphic(symbol: textSymbol)
        }()
    
        let markerGraphic: Graphic = {
            let markerSymbol = SimpleMarkerSymbol(
                style: .square,
                color: .red,
                size: 14
            )
            return Graphic(symbol: markerSymbol)
        }()
    
    }
    
    Expand
  3. Create an init function for the Model class. Create a GraphicsOverlay with the textGraphic and markerGraphic and assign it to the model's graphics overlay. This function will be called when Model is initialized.

    ContentView.swift
    Expand
    Use dark colors for code blocks
    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
    Add line.Add line.Add line.
    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
    private class Model: ObservableObject {
    
        let graphicsOverlay: GraphicsOverlay
    
        let textGraphic: Graphic = {
            let textSymbol = TextSymbol(
                text: "",
                color: .black,
                size: 14,
                horizontalAlignment: .center,
                verticalAlignment: .bottom
            )
            textSymbol.backgroundColor = .white
            return Graphic(symbol: textSymbol)
        }()
    
        let markerGraphic: Graphic = {
            let markerSymbol = SimpleMarkerSymbol(
                style: .square,
                color: .red,
                size: 14
            )
            return Graphic(symbol: markerSymbol)
        }()
    
        init() {
            graphicsOverlay = GraphicsOverlay(graphics: [textGraphic, markerGraphic])
        }
    
    }
    
    Expand

Create a locator task with geocode parameters

Geocoding is implemented with a locator, typically created by referencing a service such as the Geocoding service or, for offline geocoding, by referencing locator data contained in a mobile package. Geocoding parameters can be used to fine-tune the results, such as setting the maximum number of results or requesting additional attributes in the results.

  1. Create a LocatorTask property in the Model, named locator, based on the ArcGIS geocoding service hosted by ESRI.

    ContentView.swift
    Expand
    Use dark colors for code blocks
    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
    Add line.Add line.Add line.
    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
    private class Model: ObservableObject {
    
        let graphicsOverlay: GraphicsOverlay
    
        let locator = LocatorTask(
            url: URL(string: "https://geocode-api.arcgis.com/arcgis/rest/services/World/GeocodeServer")!
        )
    
        let textGraphic: Graphic = {
            let textSymbol = TextSymbol(
                text: "",
                color: .black,
                size: 14,
                horizontalAlignment: .center,
                verticalAlignment: .bottom
            )
            textSymbol.backgroundColor = .white
            return Graphic(symbol: textSymbol)
        }()
    
        let markerGraphic: Graphic = {
            let markerSymbol = SimpleMarkerSymbol(
                style: .square,
                color: .red,
                size: 14
            )
            return Graphic(symbol: markerSymbol)
        }()
    
        init() {
            graphicsOverlay = GraphicsOverlay(graphics: [textGraphic, markerGraphic])
        }
    
    }
    
    Expand
  2. In the ContentView, create a private String variable named searchText with the @State property wrapper. This will hold the user input and be used to perform the geocode operation.

    ContentView.swift
    Expand
    Use dark colors for code blocks
    55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    Add line.
    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
    struct ContentView: View {
    
        @StateObject private var model = Model()
    
        @State private var map = Map(basemapStyle: .arcGISImagery)
        @State private var viewpoint: Viewpoint? = Viewpoint(
            latitude: 34.02700,
            longitude: -118.80500,
            scale: 72_000
        )
    
        @State private var searchText: String = ""
    
        var body: some View {
    
            MapView(map: map, viewpoint: viewpoint, graphicsOverlays: [model.graphicsOverlay])
    
        }
    
    }
  3. Create a private, asynchronous function named geocode(with:). This function will be called when the user inputs an address.

    ContentView.swift
    Expand
    Use dark colors for code blocks
    55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    Add line.Add line.Add line.
    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
    struct ContentView: View {
    
        @StateObject private var model = Model()
    
        @State private var map = Map(basemapStyle: .arcGISImagery)
        @State private var viewpoint: Viewpoint? = Viewpoint(
            latitude: 34.02700,
            longitude: -118.80500,
            scale: 72_000
        )
    
        @State private var searchText: String = ""
    
        private func geocode(with searchText: String) async throws {
    
        }
    
        var body: some View {
    
            MapView(map: map, viewpoint: viewpoint, graphicsOverlays: [model.graphicsOverlay])
    
        }
    
    }
  4. Within the new function, create GeocodeParameters, and its attributes as follows:

    • Specify which attributes to return with addResultAttributeName. * is used to return all attributes.
    • Set the maximum number of results to be returned with maxResults. In this tutorial, only return the best match by passing in 1. Results are ordered by score, so just returning the first result will return the highest scoring result.
    • Set the spatial reference with outputSpatialReference. By default the output spatial reference is determined by the geocode service. For optimal performance when displaying the geocode result, ensure the returned coordinates match those of the map view by providing mapView.spatialReference as a parameter.
    ContentView.swift
    Expand
    Use dark colors for code blocks
    68 69 70 71 72 73 74 75
    Add line.Add line.Add line.Add line.
    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
        private func geocode(with searchText: String) async throws {
    
            let parameters = GeocodeParameters()
            parameters.addResultAttributeName("*")
            parameters.maxResults = 1
            parameters.outputSpatialReference = map.spatialReference
    
        }
    
    Expand
  5. Perform the geocode operation by calling geocode(forSearchText:using:) and supplying the search text and the geocode parameters. The result obtained from the geocode operation will be displayed as a graphic in the map view's graphics overlay.

    ContentView.swift
    Expand
    Use dark colors for code blocks
    68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
        private func geocode(with searchText: String) async throws {
    
            let parameters = GeocodeParameters()
            parameters.addResultAttributeName("*")
            parameters.maxResults = 1
            parameters.outputSpatialReference = map.spatialReference
    
            let geocodeResults = try await model.locator.geocode(forSearchText: searchText, using: parameters)
            if let firstResult = geocodeResults.first,
               let extent = firstResult.extent,
               let location = firstResult.displayLocation,
               let symbol = model.textGraphic.symbol as? TextSymbol {
                viewpoint = Viewpoint(boundingGeometry: extent)
                model.markerGraphic.geometry = location
                model.textGraphic.geometry = location
                symbol.text = firstResult.label
            }
    
        }
    
    Expand

Add a search bar to the UI

To search an address using the application, add a UI element to prompt the user for text input.

  1. In the body, add an overlay to the MapView. Set the alignment to the top, add padding all around, and set the background.

    ContentView.swift
    Expand
    Use dark colors for code blocks
    88 89 90 91 92 93 94 95 96 97 98
    Add line.Add line.Add line.Add line.Add line.
    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
        var body: some View {
    
            MapView(map: map, viewpoint: viewpoint, graphicsOverlays: [model.graphicsOverlay])
    
                .overlay(alignment: .top) {
    
                    .padding(EdgeInsets(top: 60, leading: 10, bottom: 10, trailing: 10))
                    .background(.thinMaterial, ignoresSafeAreaEdges: .horizontal)
                }
    
        }
    
    Expand
  2. Within the overlay, add the following:

    • TextField: Pass in "Enter address" as the titleKey and the searchText variable as a binding for the text parameter
    • Spacer(): Add space in between the text field and button.
    • Button: Label it "Search" and using a Task method, call the geocode(with:) function and pass in searchText
    ContentView.swift
    Expand
    Use dark colors for code blocks
    88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
        var body: some View {
    
            MapView(map: map, viewpoint: viewpoint, graphicsOverlays: [model.graphicsOverlay])
    
                .overlay(alignment: .top) {
    
                    HStack {
                        TextField("Enter address", text: $searchText)
                        Spacer()
                        Button("Search") {
                            Task {
                                try await geocode(with: searchText)
                            }
                        }
                    }
    
                    .padding(EdgeInsets(top: 60, leading: 10, bottom: 10, trailing: 10))
                    .background(.thinMaterial, ignoresSafeAreaEdges: .horizontal)
                }
    
        }
    
    Expand

Run the solution

Press Command + R to run the app.

You should see a search box at the top of the map. Search for an address by entering an address and tap the Search button. The result of the search should display on the map as a red square with the address displayed on top.

Alternatively, you can download the tutorial solution, as follows.

Option 2: Download the solution

  1. Click the Download solution link under Solution and unzip the file to a location on your machine.

  2. Open the .xcodeproj file in Xcode.

Since the downloaded solution does not contain authentication credentials, you must add the developer credentials that you created in the set up authentication section.

Set developer credentials in the solution

To allow your app users to access ArcGIS location services, pass the developer credentials that you created in the Set up authentication step to the application's ArcGISEnvironment.

Pass your API Key access token to the ArcGISEnvironment.

  1. In the Project Navigator, click MainApp.swift.

  2. Set the ArcGISEnvironment.apiKey property with your API key access token.

    MainApp.swift
    Use dark colors for code blocks
    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
            ArcGISEnvironment.apiKey = APIKey("<#YOUR-ACCESS-TOKEN#>")
    

Best Practice: The access token is stored directly in the code as a convenience for this tutorial. In a production environment we do not recommend that you store it directly in source code.

Run the solution

Press Command + R to run the app.

You should see a search box at the top of the map. Search for an address by entering an address and tap the Search button. The result of the search should display on the map as a red square with the address displayed on top.

What's next?

Learn how to use additional API features, ArcGIS location services, and ArcGIS tools in these tutorials:

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