Reverse geocoding

Reverse geocoding means converting coordinates to human-readable information. The most common use case is retrieving an address, but the Search SDK can also return more detailed information such as speed limits, postal codes, or road numbers.

Project setup

To use reverse geocoding, ensure the Search SDK has been added to the project.

Configure the project according to the project setup guide and import the necessary frameworks using the following instructions, based on your preferred package manager:

Swift Package Manager
  1. Open your App’s target and navigate to General > Frameworks, Libraries, and Embedded Content.
  2. Add the following TomTomSDK libraries from the provided code snippet. Once the project is set up, import the mentioned frameworks into your code.
import Foundation
import TomTomSDKCommon
import TomTomSDKReverseGeocoder
import TomTomSDKReverseGeocoderOnline
CocoaPods

To access the reverse geocoding module:

  1. Add TomTomSDKReverseGeocoder and TomTomSDKReverseGeocoderOnline modules to your project’s Podfile.
    TOMTOM_SDK_VERSION = '0.71.1'
    target 'YourAppTarget' do
    use_frameworks!
    pod 'TomTomSDKReverseGeocoder', TOMTOM_SDK_VERSION
    pod 'TomTomSDKReverseGeocoderOnline', TOMTOM_SDK_VERSION
    end
  2. Install the dependencies by executing the following commands in the project directory:
    pod repo update tomtom-sdk-cocoapods
    pod install --repo-update
  3. Import the following frameworks:
    import Foundation
    import TomTomSDKCommon
    import TomTomSDKReverseGeocoder
    import TomTomSDKReverseGeocoderOnline

Initializing reverse geocoding

  1. Create the ReverseGeocoder instance using ReverseGeocoder to perform online reverse geocoding requests:
let reverseGeocoderApi = try? OnlineReverseGeocoderFactory.create(apiKey: "YOUR_TOMTOM_API_KEY")

Making a reverse geocoding call

Specify the reverse geocoding request parameters with the ReverseGeocoderOptions struct. The only mandatory parameter for the call is the CLLocationCoordinate2D of the position to get more information about. You can also set optional parameters to obtain more details about the location. A detailed description of all the parameters can be found in the Reverse Geocoding documentation .

let coordinate = CLLocationCoordinate2D(latitude: 52.366425, longitude: 4.908645)
let options = ReverseGeocoderOptions(
position: coordinate,
heading: Measurement.tt.degrees(90),
radius: Measurement.tt.meters(1000)
)

Once the ReverseGeocoder and ReverseGeocoderOptions instances are created, you can perform a reverse geocoding. Parameter completion: The completion closure is called after the response to the request has been processed. If no errors occurred, ReverseGeocoderResponse contains addresses that match the geocoding request.

Here is a sample reverse geocoder request:

guard let reverseGeocoderApi else { return }
reverseGeocoderApi.reverseGeocode(options: options) { result in
// Here you can get the first address of the recognized place.
guard case let .success(response) = result,
let address = response.places.first?.place.address else {
return
}
}