我正在创建一个位置应用,我将我的位置存储在FireBase中,我在函数中获取当前坐标。但是如何将这些坐标发送到另一个在线存储它的函数。
这是我的代码:
var latPass: Double!
var longPass: Double!
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
var location=locations[0]
let span:MKCoordinateSpan=MKCoordinateSpanMake(0.01, 0.01)
var myLocation:CLLocationCoordinate2D=CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region:MKCoordinateRegion=MKCoordinateRegionMake(myLocation, span)
latPass=location.coordinate.latitude
longPass=location.coordinate.longitude
}
func post(){
let lat=latPass
let long=longPass
let post : [String: Double]=["lat":lat, "long":long]
let dataBaseRef=FIRDatabase.database().reference()
dataBaseRef.child("Location").childByAutoId().setValue(post)
}
我的 latPass & longPass 是我要传递给post()
我该怎么办?救命啊!
答案 0 :(得分:0)
您已经在班级中全局声明了它们,因此您可以像现在一样在任何地方访问它们。无需创建其他变量。
func post() {
let post : [String: Double]=["lat":latPass, "long":longPass]
let dataBaseRef=FIRDatabase.database().reference()
dataBaseRef.child("Location").childByAutoId().setValue(post)
}
否则你可以在post函数中设置参数:
func post(lat: Double, long: Double) {
let post : [String: Double]=["lat":lat, "long":long]
let dataBaseRef=FIRDatabase.database().reference()
dataBaseRef.child("Location").childByAutoId().setValue(post)
}
立即致电:
post(lat: 11.000, long: 12.000)
<强>更新强>
将您的locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
更新为:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// if you pass this guard, then you have a valid loction
guard let latitude = manager.location?.coordinate.latitude, let longitude = manager.location?.coordinate.longitude else { return }
let span:MKCoordinateSpan=MKCoordinateSpanMake(0.01, 0.01)
let myLocation:CLLocationCoordinate2D=CLLocationCoordinate2DMake(latitude, longitude)
let region:MKCoordinateRegion=MKCoordinateRegionMake(myLocation, span)
latPass = latitude
longPass = longitude
// Call post here because now you have valid a location
post()
}