【发布时间】:2023-03-29 13:25:01
【问题描述】:
我有获取用户位置的代码。我希望用户能够按下按钮并在屏幕上查看他们的坐标。我该怎么做,因为我不能直接为按钮功能调用 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)
}
【问题讨论】:
-
您的代码是否正在编译?将 myLat 和 myLong 声明为全局变量。
标签: swift swift2 uilabel core-location ibaction