Swift Reverse Geocoding – Get address from co-ordinates using MapBox Geocoder

Mapbox provides a simple tool for geocoding and reverse geocoding.

Before you begin,

Sign up for an account at mapbox.com/signup. Find your access token on your account page.

Open info.plist file and the following key, with value as the access token you received from MapBox in the first step.

<key>MGLMapboxAccessToken</key>
<string>YOUR_TOKEN</string>

Add the pod in your Podfile

pod 'MapboxGeocoder.swift', '~> 0.10'

and run ‘pod install’

Reverse geocoding

import Mapbox
let geocoder = Geocoder.shared
let address = ""

func reverseGeocodeCoordinate(coordinate: CLLocationCoordinate2D) {
    let options = ReverseGeocodeOptions(coordinate: coordinate)
           
    geocoder.geocode(options) { (placemarks, attribution, error) in
        guard let placemark = placemarks?.first else {
            return
        }
        self.address = placemark.formattedName
               
        print(placemark.imageName ?? "")
        print(placemark.genres?.joined(separator: ", ") ?? "")
        print(placemark.administrativeRegion?.name ?? "")
        print(placemark.administrativeRegion?.code ?? "")
        print(placemark.place?.wikidataItemIdentifier ?? "")
    }
}

    

Also published on Medium.

By |2019-04-04T05:18:56+00:00March 25th, 2019|Categories: iOS Tips|0 Comments

Leave A Comment