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所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)