使用Swift无法获得正确的位置

时间:2015-02-03 16:28:16

标签: swift location locationmanager

所以,我试图将我的位置放在我的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()函数的末尾。

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

1 个答案:

答案 0 :(得分:1)

你在做什么是不可能的。您可以做的就是在manager.startUpdatingLocation()内拨打refresh()并等待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()

}