Search for an address

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

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

The following are required for this tutorial:

  1. An ArcGIS account to access your API keys. If you don't have an account, sign up for free.
  2. Your system meets the system requirements.

Steps

Open the Xcode project

  1. To start the tutorial, complete the Display a map tutorial or download and unzip the solution.

  2. Open the .xcodeproj file in Xcode.

  3. If you downloaded the solution project, set your API key.

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
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 55 56 56 56 57 58 59 60 61 62 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 62 62
    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
    15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 16 17 18 19 20 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 22 23 24 25 26 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 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
    19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 20 21 22 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 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
    87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 88 89 90 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 92 92 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
    19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 20 21 22 23 23 23 23 23 24 25 26 27 28 29 30 31 32 33 34 35 35 35 35 35 35 35 35 35 35 35 35 35 35 36 36 36 36 36 36 36 36 36 36 36 36 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 14 14 14 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 -1 -2 -2 -2 -2
    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
    19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 20 21 22 23 23 23 23 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 44 44 44 44 45 45 45 45 45 45 45 45 45 45 45 45 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 23 23 23 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 7 7 7
    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
    19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 20 21 22 23 23 23 23 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 49 49 49 49 49 49 49 49 49 49 49 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 27 27 27 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 11 11 11
    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, with the Geocoding service URL.

    ContentView.swift
    Expand
    Use dark colors for code blocks
    19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 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 53 53 53 53 53 53 53 53 53 53 53 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 31 31 31 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 15 15 15
    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
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 55 56 57 58 59 60 61 62 63 64 65 66 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 68 69 70 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 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
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 70 71 72 73 74 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 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
    67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 68 69 70 71 72 73 74 74 74 74 74 74 74 74 74 74 74 74 75 75 75 75 75 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 59 59 59
    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
    67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 86 86 86 86 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 70 70 70
    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
    87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 88 89 90 91 92 93 93 93 93 93 93 93 93 93 93 93 94 95 96 97 98 98 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
    87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 108 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
  3. 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.

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