swift - IBAction 显示纬度和经度(swift)

标签 swift swift2 uilabel core-location ibaction

我有获取用户位置的代码。我希望用户能够按下按钮并在屏幕上看到他们的坐标。我该如何执行此操作,因为我无法直接调用 myLat 或 MyLong 作为按钮功能,因为它是位置管理器功能。为什么这段代码不起作用?

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

    // get most recient coordinate
    let myCoor = locations[locations.count - 1]

    //get lat & long
    var myLat = myCoor.coordinate.latitude
    let myLong = myCoor.coordinate.longitude
    let myCoor2D = CLLocationCoordinate2D(latitude: myLat, longitude: myLong)

    //set span
    let myLatDelta = 0.05
    let myLongDelta = 0.05
    let mySpan = MKCoordinateSpan(latitudeDelta: myLatDelta, longitudeDelta: myLongDelta)

    let myRegion = MKCoordinateRegion(center: myCoor2D, span: mySpan)
    self.LocationManager.stopUpdatingLocation()
 }




@IBAction func coo(sender: AnyObject) {
     self.coolat.text! = String(myLat)
     self.coolong.text! = String(myLong)
}

最佳答案

由于您的 locationManager 变量 myLat 和 myLong 对于该函数是本地的,因此只需声明两个全局变量,(a) 由 locationManager 设置,(b) 然后由 IBAction 函数拉取。

关于swift - IBAction 显示纬度和经度(swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40855440/

相关文章:

ios - 在 Swift 中正确关闭(或 "destructing") View

使用 Firebase 的 iOS 推送通知

ios - 当尝试将 UITextField 文本转换为 Int Xcode 7 时出现错误

ios - 当有足够的空间时,UILabel 自动换行会在下一行添加额外的单词

ios - 标签的内容正在堆栈 View 中被裁剪

ios - 如何根据唯一属性值对字典数组进行分组?

ios - AVAssetExportSession 导出不确定地失败,错误为 : "Operation Stopped, NSLocalizedFailureReason=The video could not be composed."

ios - 原生 iPhone 上的 Swift UITableView 黑色背景

json - 无法将类型 'Swift.Dictionary<Swift.String, protocol<>>' 的值转换为 'Swift.AnyObject'

ios - UITableViewCell 中的 UILabel - 切断信息