Works with CLGeocoder

Tram Ho

The CLGeocoder class is part of the Core Location framework and it appeared from iOS 5 and macOS 10.8. Initially, the function of the CLGeocoder class was limited to reversing the geocoding address to a coordinates. However, since iOS 8 and macOS 10.10, you can also fetch the coordinates of the addresses. In this tutorial, you learn how to do both using Xcode 9 and Swift 4.

Project Setup

Launch Xcode and create a new application based on the Tabbed Application pattern.

Name the project Geocoding, set Language to Swift and Device to iPhone.

Before we start working with the CLGeocoder class, we need to change a few things. Xcode has created two UIViewController subclasses for us, FirstViewController and SecondViewController . Rename FirstViewController to ForwardGeocodingViewController and SecondViewController to ReverseGeocodingViewController . This also means you need to update the filenames of these classes and the class names in Main.storyboard .

Get geographic coordinates with Swift

We start with obtaining geographic coordinates forwarding an address. This means we require the user to enter the address to which the application fetches coordinates.

Create the user interface

Open ForwardGeocodingViewController.swift and create six outlets as shown below. Text fields are used to enter the country, city and street of the location. The application uses these values ​​to forward the geocode of the location coordinates.

We also create an action, called when the user presses the button. This action confirms the input and begins the geocoding operation. We leave it blank for now.

As you can see, the user interface is quite basic. Make sure you connect the sockets to the corresponding UI components and do not forget to connect the event touching the internal node with geocode (_:)

Validate input

We only want to start a geocoding operation if the user’s input is valid. Open ForwardGeocodingViewController.swift and update geocode (_:) as follows.

Geocode transcoding

The API of CLGeocoder class is short and easy to use. We have three options to forward the geocode the address the user has entered in the text fields.

  • geocodeAddressString (_: completionHandler 🙂
  • geocodeAddressString (_: in: completionHandler 🙂
  • geocodeAddressDictionary ( : completionHandler 🙂 The geocodeAddressString ( : completionHandler:) method is the easiest to use, but it is also the most error-prone. Why so? We give the CLGeocoder class an address as a string. It depends on the core location framework and the web service it communicates with, to find out which address string component of the string.

A better option is to use the geocodeAddressString (_: in: completionHandler:) method. In addition to the address string, the method accepts a CLRegion instance to further narrow the position that the user is interested in. This is useful because it avoids mismatches due to similarly named locations.

The third option is to use the geocodeAddressDictionary (_: completionHandler:) method. This method allows us to specify an address dictionary as determined by the Address box, providing the Core Location frame with more accurate information about the location we are interested in.

In this article, we use the geocodeAddressString ( : completionHandler:) method . See the updated deployment of geocode ( 🙂 method below.

We create an address string from the user’s input and call geocodeAddressString ( : completionHandler:) on an CLGeocoder example. The completion handler, the second parameter of geocodeAddressString ( : completionHandler:), accepts two parameters, an optional array of CLPlacemark versions and an Error object. In the completion handler, we call a helper method, processResponse (withPlacemarks: error:).

After starting geocoding operations, we hide the button and show users the activity indicator view. This shows the user the progress of geocoding operations.

Declare a lazy attribute, geocoder, type CLGeocoder.

Although it is not difficult to implement processResponse (withPlacemarks: error:), you may find that we need to perform some checks before we can access the coordinates of the location we are interested in. Why so?

If a user enters a bogus address or makes a typo, the Core Location framework may not be able to find a location for the address.

Run the application and observe the results.

summary

The article has been translated from https://cocoacasts.com/forward-geocoding-with-clgeocoder Hope the knowledge in the article will bring you useful things. Hello and see you again in the next article.

Share the news now

Source : Viblo