位置不断更新问题iOS Swift

位置不断更新问题iOS Swift,第1张

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

import UIKitimport CoreLocationclass 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!) {    }}
解决方法 将locationManager.startUpdatingLocation()移动到findMyLocation函数.这将在按下按钮时启动locationManager并开始调用dIDUpdateLocations在if startLocation == nil中添加locationManager.stopUpdatingLocation()这将在你设置startLocation var后停止locationManager.每次用户按下按钮时,该过程将再次运行.

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

更新:

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

import UIKitimport CoreLocationclass 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!,dIDFailWithError error: NSError!) {        println("error with location manager: " + error.description)    }}
总结

以上是内存溢出为你收集整理的位置不断更新问题iOS Swift全部内容,希望文章能够帮你解决位置不断更新问题iOS Swift所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1058626.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存