Geocode a Street Address in Swift

Does your business require precise location data to fulfill their services? If so, the following API can simplify the retrieval of that information by taking a text string input for an address and returning the geographical the latitude and longitude of the location. This will provide exact coordinates, which is essential for applications like locator, delivery, and ride-sharing platforms, as well as several others.

To perform the operation, simply plugin the street address, city, and state/province to the below code — if you have the postal code and country name/code, you can input that as well.

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
var semaphore = DispatchSemaphore (value: 0)let parameters = "{\n \"StreetAddress\": \"<string>\",\n \"City\": \"<string>\",\n \"StateOrProvince\": \"<string>\",\n \"PostalCode\": \"<string>\",\n \"CountryFullName\": \"<string>\",\n \"CountryCode\": \"<string>\"\n}"
let postData = .utf8)
var request = URLRequest(url: URL(string: "")!,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))
print(String(data: data, encoding: .utf8)!)

You will now have your coordinates to do with what you will! If you don’t already have an API key, you can retrieve one by registering for a free account on the Cloudmersive website; this will provide access to 800 calls/month.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store