位置不断更新问题iOS Swift

Sau*_*pan 4 location ios swift

我正在获取用户当前位置并将其作为println()删除.我的想法是,我要按一个按钮来获取新位置,但是目前应用程序不断更新(每秒).我已经尝试在我的IBAction中移动getLocation()函数但是崩溃了.我已经更新了info.plist,这不是问题.继承人代码:

    import UIKit
import CoreLocation


class ViewController: UIViewController, CLLocationManagerDelegate{

    @IBOutlet var latitude : UILabel!
    @IBOutlet var longitude : UILabel!

    var locationManager = CLLocationManager()
    var startLocation: CLLocation!


    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager = CLLocationManager()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()

    }

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

    @IBAction func findMyLocation(sender: AnyObject){
        startLocation = nil
        locationManager.startUpdatingLocation()

    }

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

        var userLocation:AnyObject = locations[0] as! CLLocation

        var strlat = String(format: "%.4f", userLocation.coordinate.latitude)
        var strlong = String(format: "%.4f",userLocation.coordinate.longitude)

        latitude.text = String(format: "%.4f", userLocation.coordinate.latitude)
        longitude.text = String(format: "%.4f",userLocation.coordinate.longitude)

        println("latitude: " + strlat)
        println("longitide: " + strlong)

        if startLocation == nil {
            startLocation = userLocation as! CLLocation
            locationManager.stopUpdatingLocation()
        }



    }

    func locationManager(manager: CLLocationManager!,
        didFailWithError error: NSError!) {

    }
}
Run Code Online (Sandbox Code Playgroud)

rmp*_*rmp 7

移动locationManager.startUpdatingLocation()到您的findMyLocation功能.这将在按下按钮时启动locationManager并开始调用didUpdateLocations Inside你的if startLocation == nil添加,locationManager.stopUpdatingLocation()这将在你设置startLocationvar 后停止locationManager .每次用户按下按钮时,该过程将再次运行.

还有一点需要注意,您应该didUpdateLocations在使用之前添加更多代码以检查位置的准确性和时间戳,因为它可能不是您尝试执行的有效/准确位置.

更新:

有机会验证,代码将与建议的更改一起使用.这是您的最终代码应该是什么样子.我还假设您已经为位置服务设置了plist条目,并且您的模拟器设置为模拟locaitons.

import UIKit
import CoreLocation


class ViewController: UIViewController, CLLocationManagerDelegate{

    @IBOutlet var latitude : UILabel!
    @IBOutlet var longitude : UILabel!

    var locationManager : CLLocationManager! = CLLocationManager()
    var startLocation: CLLocation!


    override func viewDidLoad() {
        super.viewDidLoad()

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

    }

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

    @IBAction func findMyLocation(sender: AnyObject){
        startLocation = nil
        locationManager.startUpdatingLocation()

    }

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

        var userLocation:AnyObject = locations[0] as! CLLocation

        var strlat = String(format: "%.4f", userLocation.coordinate.latitude)
        var strlong = String(format: "%.4f",userLocation.coordinate.longitude)

        latitude.text = String(format: "%.4f", userLocation.coordinate.latitude)
        longitude.text = String(format: "%.4f",userLocation.coordinate.longitude)

        println("latitude: " + strlat)
        println("longitide: " + strlong)

        if startLocation == nil {
            startLocation = userLocation as! CLLocation
            locationManager.stopUpdatingLocation()
        }



    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {

        println("error with location manager: " + error.description)

    }


}
Run Code Online (Sandbox Code Playgroud)