0

I'm currently able to determine an user's lat/long and access the coordinates. How would I go about reverse geocoding the coordinates to determine the city the user is currently in so that I could display it?

Here's my current LocationManager file

import Foundation
import MapKit

class LocationManager: NSObject, ObservableObject {

private let locationManager = CLLocationManager()
@Published var location: CLLocation? = nil

override init() {
    super.init()
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.distanceFilter = kCLDistanceFilterNone
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
    }
}


class ViewController: UIViewController, CLLocationManagerDelegate {

    let locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
    
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.delegate = self
}

// MARK: - CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    switch status {
    case .notDetermined:
        print("notDetermined")
        manager.requestWhenInUseAuthorization()
    default:
        break
        }
    }
}

extension LocationManager: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    
    guard let location = locations.last else {
        return
    }
    
    self.location = location
}


}

I'd like to pass the city information to one of my views and output the city in a text element. i.e. "Explore Orlando"

  • Does this answer your question? [Convert coordinates to City name?](https://stackoverflow.com/questions/27735835/convert-coordinates-to-city-name) – El Tomato Jul 02 '20 at 23:52

1 Answers1

1

You can use CLGeocoder for this

// Add below code to get address for touch coordinates.
        let geoCoder = CLGeocoder()
        let location = CLLocation(latitude: YOUR_LATITUDE, longitude: YOUR_LONGITUDE)
        geoCoder.reverseGeocodeLocation(location, completionHandler:
            {
                placemarks, error -> Void in

                // Place details
                guard let placeMark = placemarks?.first else { return }

                // Location name
                if let locationName = placeMark.location {
                    print(locationName)
                }
                // Street address
                if let street = placeMark.thoroughfare {
                    print(street)
                }
                // City
                if let city = placeMark.subAdministrativeArea {
                    print(city)
                }
                // Zip code
                if let zip = placeMark.isoCountryCode {
                    print(zip)
                }
                // Country
                if let country = placeMark.country {
                    print(country)
                }
        })

credits to Convert coordinates to City name?

Ludyem
  • 1,709
  • 1
  • 18
  • 33
  • Not sure if this makes a difference, but I'm not trying to grab the coordinates based on user interaction with a map. I want to get the location information on app launch just to show personalization. Like if it was a weather app and when the app launches text would show "Check local weather in (get coordinates and display user's current city). – Jay Hamilton Jul 04 '20 at 14:43
  • 1
    @JayHamilton you don't need the user to interact with a map, just add this method inside your didUpdateLocations and use the user location that you get in there – Ludyem Jul 05 '20 at 13:23