banner image 1 banner image 2

Forward and Reverse Geocoding in iOS

December 27, 2022
4 mins
command
blog-img 1
Praveen Apk
Author

This article helps you understand geocoding in iOS using the CLGeocoder class.

By Praveen Apk- The architect of my life


Since the product delivery is directly related to the Pin-code, we would need the user Pin-code. We use Pin-code for multiple purposes like calculating the expected delivery time, features available for a particular area like fast delivery, try-at-home options, etc.,

Forward and Reverse Geocoding in iOS

Generally, we ask the user to enter the Pin-code and to get an accurate Pin-code we use, user’s location. Using a user’s location to get Pin-code is more convenient for users also. We will just ask for the user’s permission to use their location.

Forward and Reverse Geocoding in iOS

Once a user permits location we will use the CLGeocoder class to do reverse and forward geocode requests.

CLGeocoder is part of iOS’s CoreLocation framework and it’s available from iOS 5.

The CLGeocoder class provides services for converting between a coordinate (specified as a latitude and longitude) and the user-friendly representation of that coordinate like street, city, country, etc., This class handles both reverse and forward geocoding requests.

Since we are focussing on the Pincode, we will see how to use both requests using the Pincode.

  • Reverse geocoding to get pincode from the location.
  • Forward geocoding to get location from pincode.

Reverse Geocoding

First, we will see how to use reverse geocoding. Reverse-geocoding requests take a latitude and longitude value and find a user-readable address. Below are the steps involved in it.

Create an instance of geocoder.

// Geocoder instance
var geocoder = CLGeocoder()

Once the user gives permission for location access, we will get the user’s coordinates. Create a CLLocation instance using the coordinate values.

// Create a location using latitude and longitude
let location = CLLocation(latitude: latitude, longitude: longitude)

Call the geocoder’s reverseGeocodeLocation method with the location instance.

var pincode = String()
geocoder.reverseGeocodeLocation(location) { placemarks, error in
if error == nil {
// Optional binding for placemarks array
if let placemarks = placemarks, placemarks.count > 0 {
// Get the first placemark value
if let place = placemarks.first {
// Set the pincode value
pincode = place.postalCode
}
}
} else {
// Handle error
print(error.debugDescription)
}
}

reverseGeocodeLocation method’s completion handler is of type ([CLPlacemark]?, Error?) -> Void We will get an optional array of placemarks and an error object.

If the location passed to the method is invalid, the geocoder will throw an error. So we need to check for errors before proceeding.

Once we ensure that there is no error, we need to check the placemarks array. Since it is of type optional array, we are using optional binding to unwrap the array.

Next, we are checking for an empty array by checking the size of the array. After that, we are fetching the first item from the placemarks. Sometimes a single location might have multiple addresses. We are using the first placemarks value from the array.

CLPlacemark is having multiple user-friendly descriptions of addresses. We are using the postalCode value.

Once we get the Pincode from the location, we can store it in the user session and use it going forward.

Forward Geocoding

Now we will see the forward geocoding. Forward-geocoding requests take a user-readable address and find the corresponding latitude and longitude values. Below are the steps involved in it.

For forward geocoding also we will be needing an instance of geocoder.

// Geocoder instance
var geocoder = CLGeocoder()

Here we are going to pass the user’s pincode to the geocoder’s geocodeAddressString method.

var pincodeLocation: CLLocation?
geocoder.geocodeAddressString(pincode) { (placemarks, error) in
if error == nil {
// Optional binding for placemarks array
if let placemarks = placemarks, placemarks.count > 0 {
// Get the location from first placemark
if let location = placemarks.first?.location {
// Set the pincode location
pincodeLocation = location
}
}
} else {
// Handle error
print(error.debugDescription)
}
}

The completion handler is the same as the reverseGeocodeLocation method ([CLPlacemark]?, Error?) -> VoidWe will make the same kind of error checks, optional checks, and empty checks for the placemarks array.

Once we get the first placemark value, we will access the location value of type CLLocation that will have lat and long for the given pincode. The coordinates can be used for other purposes within our App.

Conclusion

We have seen how we can use the CLGeocoder library to do both reverse geocoding and forward geocoding in iOS.


References

[embed]https://developer.apple.com/documentation/corelocation/clgeocoder[/embed][embed]https://developer.apple.com/documentation/corelocation/clgeocoder[/embed][embed]https://developer.apple.com/documentation/corelocation/clgeocoder[/embed][embed]https://developer.apple.com/documentation/corelocation/clgeocoder[/embed][embed]https://developer.apple.com/documentation/corelocation/clgeocoder[/embed]

Meet the team!

Author

Praveen Apk

Editor

Seema Jain


We at CaratLane are solving some of the most intriguing challenges to make our mark in the relatively uncharted omnichannel jewellery industry. If you are interested in tackling such obstacles, feel free to drop your updated resume/CV to careers@caratlane.com
blog-img 2

Discussions

blog-img 3
5 mins
May 17, 2023
Sharing Data Between Controllers: Best Practices S...

This article will help you to understand the diffe

By Naveen C

blog-img 3
5 mins
March 21, 2023
Understanding Auto Layout and Constraints in Swift...

This article gives you an easy way of understandin

By Ramasamy P