无法使用 Swift 获取正确位置

Can't get location right using Swift

所以,我试图在 ViewController.swift.

的函数中获取我的位置

当我这样尝试时,我使用的代码有效:

import UIKit
import CoreLocation


class ViewController: UIViewController, CLLocationManagerDelegate {

    var manager : CLLocationManager!


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        manager = CLLocationManager()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyHundredMeters
        manager.requestAlwaysAuthorization()
        manager.startUpdatingLocation()
    }

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

        var locValue : CLLocationCoordinate2D
        locValue = manager.location.coordinate
        println("Coordinates = \(locValue.longitude) + \(locValue.latitude)")
        manager.stopUpdatingLocation()
    }

但我想要做的是在按下一个切换功能的按钮时获取它:

var manager : CLLocationManager!
    override func viewDidLoad() {
        super.viewDidLoad()

        manager = CLLocationManager()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyHundredMeters
        manager.requestAlwaysAuthorization()
        manager.startUpdatingLocation()
        }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        var locValue: CLLocationCoordinate2D = manager.location.coordinate
        println("locations = \(locValue.longitude) \(locValue.latitude)")
        manager.stopUpdatingLocation()
    }

    @IBAction func refresh() {
        getWeatherData()
        refreshButton.hidden = true
        acitivityIndicatorView.hidden = false
        acitivityIndicatorView.startAnimating()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
       }

        func getWeatherData() -> Void {

 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject]!) {
                var locValue: CLLocationCoordinate2D = manager.location.coordinate
                println("locations = \(locValue.longitude) \(locValue.latitude)")
                manager.stopUpdatingLocation()

在使用断点时我意识到它到达了 func locationManager 但它跳转到了我的 getweatherdata() 函数的末尾。

我不明白为什么它不起作用。

你正在做的事情是不可能的。您唯一可以做的就是在 refresh() 中调用 manager.startUpdatingLocation() 并等待 didUpdateLocations 被调用并使用更新的位置数据。

@IBAction func refresh() {
    manager.startUpdatingLocation() 
    refreshButton.hidden = true
    acitivityIndicatorView.hidden = false
    activityIndicatorView.hidesWhenStopped = true
    acitivityIndicatorView.startAnimating()
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    var locValue: CLLocationCoordinate2D = manager.location.coordinate
    println("locations = \(locValue.longitude) \(locValue.latitude)")
    manager.stopUpdatingLocation()
    refreshButton.hidden = false
    acitivityIndicatorView.stopAnimating()

}