位置不断更新问题iOS Swift

时间:2015-08-03 14:43:57

标签: ios swift location

我正在获取用户当前位置并将其作为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!) {

    }
}

1 个答案:

答案 0 :(得分:7)

locationManager.startUpdatingLocation()移至findMyLocation功能。这将在按下按钮时启动locationManager并开始调用didUpdateLocationsif startLocation == nil添加locationManager.stopUpdatingLocation()内部,这将在您设置startLocation var后停止locationManager。每次用户按下按钮时,该过程将再次运行。

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

<强>更新

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

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)

    }


}