Working with details of location in Swift

Sai Durga Mahesh
Geek Culture
Published in
2 min readJul 26, 2022
Photo by Clay Banks on Unsplash

In this post, we will fetch the details of location based on coordinates and vice versa. This two pieces of code are very useful while working with MapKit.

Fetching details based on coordinates

CLLocationManager is used to fetch the coordinates of User. We will make our view model delegate to CLLocationManager. Then CLLocationManager will provide us with coordinates using ‘didUpdateLocations’.

Once we get the coordinate of user, we will use CLGeocoder’s reverseGeocodeLocation function to get the CLPlacemark. From this placemark, we will decode location details as follows:

func update() {let latitude = location?.latitude ?? 0let longitude = location?.longitude ?? 0self.region = MKCoordinateRegion(center:CLLocationCoordinate2D(latitude:                                                                       CLLocationDegrees(latitude), longitude: CLLocationDegrees(longitude)), span:                                 MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: latitude, longitude: longitude)) { placemarks, error inguard let placemark = placemarks?.first else {return}let reversedGeoLocation = GeoLocation(with: placemark)
self.name = reversedGeoLocation.name

}

}

The GeoLocation is Custom Model which we use to decode details of placemark.

struct GeoLocation {let name: Stringlet streetName: Stringlet city: Stringlet state: Stringlet zipCode: Stringlet country: Stringinit(with placemark: CLPlacemark) {self.name           = placemark.name ?? ""self.streetName     = placemark.thoroughfare ?? ""self.city           = placemark.locality ?? ""self.state          = placemark.administrativeArea ?? ""self.zipCode        = placemark.postalCode ?? ""self.country        = placemark.country ?? ""}}

In this way, we can fetch details of location using coordinates.

Fetching Coordinates based on Address

When user enters the address in any way, we can use CLGeocoder to geocode the address string as following:

func reverseUpdate() {let geocoder = CLGeocoder()geocoder.geocodeAddressString(name) { placemarks, error in

guard error == nil else { return}

guard let placemark = placemarks?[0] else {return}
let coord = placemark.location?.coordinate ?? CLLocationCoordinate2D(latitude:
CLLocationDegrees(0), longitude: CLLocationDegrees(0))
self.region = MKCoordinateRegion(center: coord, span:
MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
self.location = CLLocationCoordinate2D(latitude: placemark.location?.coordinate.latitude ?? 0, longitude: placemark.location?.coordinate.longitude ?? 0)

}

The CLGeocoder will give CLPlacemark with which we will get the coordinates of location.

The complete view model:

The following code can be used along with the code from post:

You might also feel this post useful:

https://medium.com/@maheshsai252/search-using-mapkit-in-swiftui-636426836ab

--

--

Sai Durga Mahesh
Geek Culture

Using Data Science to provide better solutions to real word problems