1

Hello my coordinate extraction code is s below. I was looking for help on the reverse geolocation. Thanks in advance :)

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var citybtn: UIButton!
@IBOutlet weak var citylbl: UILabel!

let locationManager = CLLocationManager()
var currentLocation : CLLocation!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // Used to start getting the users location
    //let locationManager = CLLocationManager()


    // For use when the app is open
    //locationManager.requestAlwaysAuthorization()

    // If location services is enabled get the users location
    //if CLLocationManager.locationServicesEnabled() {
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest // You can change the locaiton accuary here.
    locationManager.requestWhenInUseAuthorization()



}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)

    locationAuthStatus()
}

func locationAuthStatus() {

    if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {

        currentLocation = locationManager.location
        print(currentLocation.coordinate.latitude)
        print(currentLocation.coordinate.longitude)

    } else {
        locationManager.requestWhenInUseAuthorization()
        locationAuthStatus()
    }
}



@IBAction func buttonpresses(_ sender: Any) {

}



}
Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36

2 Answers2

0
let geoCoder = CLGeocoder()
     geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in

                // Place details
                var placeMark: CLPlacemark!
                placeMark = placemarks?[0]

                // Address dictionary
                  print(placeMark.addressDictionary)

                            // Location name
                            if let locationName = placeMark.addressDictionary!["Name"] as? NSString {
                                print(locationName)
                            }

                            // Street address
                            if let street = placeMark.addressDictionary!["Thoroughfare"] as? NSString {
                                print(street)
                            }

                if placeMark != nil{
                    // City
                    if let city = placeMark.addressDictionary!["City"] as? NSString {
                        // print(city)
                        self.locCity = city as String
                    }

                                // Zip code
                                if let zip = placeMark.addressDictionary!["ZIP"] as? NSString {
                                    print(zip)
                                }

                    // Country
                    if let country = placeMark.addressDictionary!["Country"] as? NSString {
                        print(country)

                    }



                }

            })
Mohamed Helmy
  • 821
  • 1
  • 9
  • 20
0

Try This worked for me

you will get everything here 

" let pm = placemarks[0] as! CLPlacemark "