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

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

}



}

最佳答案

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)

                    }



                }

            })

关于ios - 如何从坐标中提取用户的城市,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44760797/

相关文章:

ios - Xamarin.iOS中的蓝牙背景扫描

objective-c - iOS:访问 XML 数据记录的简便方法?

iOS 启动屏幕 Storyboard与启动图像

ios - 为什么 tableView 方法按原样命名?

swift - 导入到 XCode 的 Collada (.dae) 文件显示没有纹理

iphone - 在 iOS4 上运行的 iPhone 4 或 3GS 上可以进行后台 GPS\位置记录吗

html - 在ios7中以html表格格式导出数据并制作电子邮件正文

ios - 如何以编程方式检测 iPhone 6 或 iPhone 6 Plus View 尺寸模式

android - 2 部手机返回完全相同的 GPS 坐标的概率,在 Android 上使用 Network_Provider

ios - 位置更新并将位置发送到服务器导致 iPhone 发热并自动重启