【问题标题】:How to get latitude and longitude of a user's location in iOS如何在iOS中获取用户位置的经纬度
【发布时间】:2015-04-15 07:45:06
【问题描述】:

我是 Swift 新手,我需要获取用户的当前位置。我的意思是我需要得到纬度和经度。我试过这个:

class ViewController: UIViewController, CLLocationManagerDelegate{

    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.startUpdatingLocation()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

        CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error) -> Void in

            if (error != nil) {
                println("ERROR:" + error.localizedDescription)
                return
            }

            if placemarks.count > 0 {
                let pm = placemarks[0] as CLPlacemark
                self.displayLocationInfo(pm)
            } else {
                println("Error with data")
            }
        })
    }

    func displayLocationInfo(placemark: CLPlacemark) {
        //  self.locationManager.stopUpdatingLocation()

        println(placemark.locality)
        println(placemark.postalCode)
        println(placemark.administrativeArea)
        println(placemark.country)
        println(placemark.location)
    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError) {
        println("Error:" + error.localizedDescription)
    }
}

在这里我可以得到坐标,但它看起来像:

+/- 100.00m(速度 -1.00 mps / 路线 -1.00)@ 2/14/15,10:48:14 AM 莫斯科标准时间

我怎样才能只检索纬度和经度?

【问题讨论】:

    标签: ios swift cllocationmanager cllocation


    【解决方案1】:

    为 Swift 3.x 和 Swift 4.x 更新了代码

    我可以看到您在代码中使用了print(placemark.location)

    因此,如果您只想获得纬度,请使用此代码

    print(placemark.location.coordinate.latitude)
    

    如果您只想获取经度,请使用此代码

    print(placemark.location.coordinate.longitude)
    

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      您可以期待多次拨打didUpdateLocations,并且准确性会随着时间的推移而提高(假设您在 GPS 接收良好的当地 - 户外,没有被高层建筑包围)。您可以直接从 locations 数组中的 CLLocation 对象访问纬度和经度。

      let location = locations[locations.count-1] as CLLocation;
      println("\(location.latitude) \(location.longitude)");
      

      【讨论】:

        【解决方案3】:

        func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)

        您的locations:[AnyObject]! 实际上是[CLLocation] 只需获取其最后一个对象并使用 CLLocation 的 coordinate 属性即可。

        https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocation_Class/index.html

        【讨论】:

        • 好吧,现在我需要获取街道名称和最近的建筑物的编号。我该怎么做?
        • 我不知道如何使用 MapKit,我使用 google map sdk。事情就是这样,您必须获取所需位置的地址(可能是 CLPlaceMark ?)并提取这些数据,或者甚至为您提取它们。 developer.apple.com/library/mac/documentation/CoreLocation/…
        猜你喜欢
        • 1970-01-01
        • 2012-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-26
        • 1970-01-01
        • 1970-01-01
        • 2012-09-26
        相关资源
        最近更新 更多