我有这种情况:
第一个视图,在这个视图中,我得到了当前位置,并将纬度和经度保存在两个双精度类型变量中:doublelat
和doublelng
import SwiftUI
struct CoordinateView: View {
@ObservedObject var locationManager = LocationManager()
@State private var gotopage = false
var userLatitude: String {
let lat = String(format: "%.6f", locationManager.lastLocation?.coordinate.latitude ?? 0)
return "\(lat)"
}
var userLongitude: String {
let lng = String(format: "%.6f", locationManager.lastLocation?.coordinate.longitude ?? 0)
return "\(lng)"
}
var doubleLat : Double {
return locationManager.lastLocation?.coordinate.latitude ?? 0
}
var doubleLng : Double {
return locationManager.lastLocation?.coordinate.longitude ?? 0
}
private var getcoordinates: Bool {
if locationManager.statusCode == 0 {
return false
} else {
return true
}
}
var body: some View {
VStack {
HStack() {
Text("Your position")
.font(.headline)
Spacer()
}
VStack(alignment: .leading) {
HStack(){
Text("Lat")
Spacer()
Text("\(userLatitude)")
}.padding(5)
HStack(){
Text("Long")
Spacer()
Text("\(userLongitude)")
}.padding(5)
}
if getcoordinates {
HStack(alignment: .center){
Button("Go to next page") {
self.gotopage = true
}
}.padding()
VStack {
if self.gotopage {
AddressView(latitude: self.doubleLat, longitude: self.doubleLng)
}
}
}
}
.padding()
}
};
struct CoordinateView_Previews: PreviewProvider {
static var previews: some View {
CoordinateView()
}
}
现在只需按下按钮Button("Go to next page")
,去查看AddressView(latitude: self.doubleLat, longitude: self.doubleLng)
,有两个参数纬度和经度。
这是地址视图:
import SwiftUI
struct AddressView: View {
var latitude: Double
var longitude: Double
@ObservedObject var apiManager = APIManager(lat: <latitude>, lng: <longitude>)
var body: some View {
.....
.....
}
struct AddressView_Previews: PreviewProvider {
static var previews: some View {
AddressView(latitude: 14.564378, longitude: 42.674532)
} }
在第二个视图中,我必须从该声明中的前一个视图传递两个参数(纬度和经度):
@ObservedObject var apiManager = APIManager(lat: <latitude>, lng: <longitude>)
我能怎么做?
谢谢你。