位置经理:如何获得用户Lats and Longs

时间:2014-11-10 11:26:11

标签: ios xcode swift

我在标签中显示我的纬度和经度时出现问题,我已经通过互联网搜索并编译了所有结果,这是我现在所得到的。

import UIKit
import CoreLocation

var locationManager = CLLocationManager()

class HomeVC: UIViewController,CLLocationManagerDelegate {

    let locationManager = CLLocationManager()

    @IBOutlet var myLong: UILabel!
    @IBOutlet var myLat: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()

        self.locationManager.requestAlwaysAuthorization()

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

        // Do any additional setup after loading the view.
    }

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

    }

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



    @IBAction func myLocation(sender: UIButton) {

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

            var locValue:CLLocationCoordinate2D = manager.location.coordinate

            var myLatString = NSString(format:"%f",locValue.latitude) as String
            self.myLat.text = myLatString

            var myLongString = NSString(format:"%f",locValue.longitude) as String
            self.myLong.text = myLongString

        }

    }

我还添加" NSLocationAlwaysUsageDescription"总是使用。现在我的问题是按下按钮时没有发生任何事情,它现在显示我的纬度和经度。我的代码有什么问题?谢谢!

2 个答案:

答案 0 :(得分:0)

你应该实现这个:

  optional func locationManager(_ manager: CLLocationManager!,
       didUpdateLocations locations: [AnyObject]!)
{
   // in locations you receive CLLocation objects.
}

答案 1 :(得分:0)

将类设置为locationManager的委托后,当您调用locationManager.startUpdatingLocation()时,将在更新位置时自动调用方法didUpdateLocations。因此,您不应将didUpdateLocations放在按钮事件中。

除此之外,您已在代码中定义了两次locationManager。另外,请使用以下代码从didUpdateLocations获取位置坐标。

也许你想在按下按钮时调用locationManager.startUpdatingLocation(),并在locationManager调用didUpdateLocations时获取位置。这个方法应该在你的类中以这种方式实现:

import UIKit
import CoreLocation

class HomeVC: UIViewController,CLLocationManagerDelegate {

var locationManager: CLLocationManager!

@IBOutlet var myLong: UILabel!
@IBOutlet var myLat: UILabel!


override func viewDidLoad() {
    super.viewDidLoad()

    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()

    // Do any additional setup after loading the view.
}

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


}

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

@IBAction func myLocation(sender: UIButton) {
    // Maybe start updating location from here. it depends on the logic of your app.

}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {   
    var locationArray = locations as NSArray
    var currentLocation = locationArray.lastObject as CLLocation
    var locValue = currentLocation.coordinate

    var myLatString = NSString(format:"%f",locValue.latitude) as String
    self.myLat.text = myLatString

    var myLongString = NSString(format:"%f",locValue.longitude) as String
    self.myLong.text = myLongString

}