Overview

You will learn: how to find an address or place using the ArcGIS World Geocoding Service.

With the ArcGIS Runtime SDK for iOS you can easily find places and addresses all around the world. The process of matching locations or place names on the map to an address is referred to as geocoding. Your user can specify a location of interest or street address and see it on the map. You can use the ArcGIS World Geocoding Service as a locator which has the ability to search for places, business names and addresses, and supports many different input formats. You can also perform reverse geocoding where you pass in a point location or latitude and longitude and the locator will return the address it finds. With this functionality you can build powerful apps that allow users to easily find what they are looking for.

Locating addresses is a complex topic. Refer to Search for places (geocoding) if you would like to read more about the process and options.

This lab is a bit complex because you will define a user interface to handle user interaction. This demonstrates one of many possible methods for requesting user input. The method used here places the search bar at the top of the iOS device's screen.

Before you begin

Make a copy of the finished Create a starter app lab (or download here) and open the .xcodeproj file in Xcode.

Steps

Add a Search Bar to the app

  1. In the project navigator, find the Main.storyboard file. Add a Search Bar above the Map View (if you prefer to start with the UI already wired up, use this project and skip ahead to Search for an address below.).

    Add a search bar

  2. Right-click on the Search Bar and drag the delegate outlet to the View Controller.

  3. In the project navigator, find the ViewController.swift file. Update the class definition to specify that it implements the UISearchBarDelegate protocol.

    class ViewController: UIViewController, UISearchBarDelegate {
    

Search for an address

  1. Create an AGSLocatorTask instance variable pointing to the ArcGIS Online World Geocoder Service.

        @IBOutlet weak var mapView: AGSMapView!
    
        /* ADD */
        var geocoder:AGSLocatorTask = AGSLocatorTask(url: URL(string:
              "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer")!)
        /* ADD */
    
        override func viewDidLoad() {
    
  2. Add a searchBarSearchButtonClicked handler for UISearchBarDelegate to zoom the map to a search result. You will call the geocoder's geocode() method and handle the response:

     func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
         geocoder.geocode(withSearchText: searchText) { (results, error) in
             guard error == nil else {
                 print("Error geocoding '\(searchText)': \(error!.localizedDescription)")
                 return
             }
    
             guard let firstResult = results?.first, let extent = firstResult.extent else {
                 let alert = UIAlertController(title: "Nothing found",
                                               message: "No results found for \(searchText)",
                                               preferredStyle: .alert)
                 alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: { _ in
                     alert.dismiss(animated: true)
                 }))
                 self.present(alert, animated: true)
    
                 return
             }
    
             self.mapView.setViewpointGeometry(extent)
         }
     }
    
  3. Now press Command-R to run the app in the iOS Simulator.

Congratulations, you're done!

The map should load and display a search bar at the top. Enter some text in the search bar and press enter. The map will zoom to the first geocoded result.

Your project should look like this.

Challenge

Show a graphic

The map zooms to the search result, but there is nothing placed on the map to show the actual location. Try using the search result's displayLocation property to create a point-based AGSGraphic to display on the map. You will need to add an AGSGraphicsOverlay to the mapView to display the graphic.

Multiple results

The geocode() method has a version that takes additional parameters. Try using AGSGeocodeParameters and varying setMaxResults to show the user multiple possible matches. Can you come up with a UI that allows the user to select a possible match?

Geocoding parameters

Can you discover other geocoding parameters available to the locator task? For example, limit the search area to the visible extent of the map view.

Show suggestions

The ArcGIS Online geocoder includes Suggestions functionality to return suggestions for a partially-typed search string. See if you can come up with a UI to present suggestions to the user as they type in the search bar. Investigate the AGSGeocoder.suggest() methods to get AGSSuggestResults. You then pass the selected suggestion to the AGSLocatorTask. This Example App might provide some inspiration.