Reverse Geocode an Address in Swift

Cloudmersive
1 min readJun 18, 2021

--

If your website or application offers location-based services, reverse geocoding is a critical component to include. The reverse geocoding process converts latitude and longitude coordinates to a normalized, readable address that can be reached on a map. To automate this otherwise tedious process, you can use the following API in Swift to instantly convert input location coordinates.

To call the API function, simply input your coordinates and API key into the below code:

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
var semaphore = DispatchSemaphore (value: 0)let parameters = "{\n \"Latitude\": \"<double>\",\n \"Longitude\": \"<double>\"\n}"
let postData = parameters.data(using: .utf8)
var request = URLRequest(url: URL(string: "https://api.cloudmersive.com/validate/address/geocode/reverse")!,timeoutInterval: Double.infinity)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("YOUR-API-KEY-HERE", forHTTPHeaderField: "Apikey")
request.httpMethod = "POST"
request.httpBody = postData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
semaphore.signal()
return
}
print(String(data: data, encoding: .utf8)!)
semaphore.signal()
}
task.resume()
semaphore.wait()

This process will improve efficiency and automation of your business, with very little effort — not a bad deal.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet