如何从坐标中提取用户的城市

时间:2017-06-26 13:23:33

标签: ios swift gps location city

您好我的坐标提取代码如下。我正在寻找有关反向地理定位的帮助。在此先感谢:)

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) {

}



}

2 个答案:

答案 0 :(得分: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)

                    }



                }

            })

答案 1 :(得分:0)

Try This worked for me

you will get everything here 

“让pm = placemarks [0]为!CLPlacemark”