我的代码获取用户位置。我希望用户能够按下按钮并在屏幕上查看其坐标。我该执行此操作,因为我无法将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)
}
由于您的位置manager变量mylat和mylong是本地唯一的,只需声明两个由位置manager设置的全局变量,而(b)为(b)然后通过IBACTION函数拉动。
声明 mylat 和 mylong 在功能外部。您可以在 locationManager()内更新这些值,这些值可以在标签中使用。