如何在程序中的其他地方获取可变输出|斯威夫特
因此,我有一个程序,可以将用户的输入转换为纬度和经度坐标。我想使用该坐标,但我真的不知道如何在程序中的任何地方称呼它。任何帮助将不胜感激!代码在下面:)
import SwiftUI
import MapKit
struct ContentView: View {
let geocoder = CLGeocoder()
@State private var address = ""
var body: some View {
VStack {
TextField("Enter Location", text: $address)
Button {
geocoder.geocodeAddressString(address, completionHandler: {(placemarks, error) -> Void in
if((error) != nil){
print("Error", error ?? "")
}
if let placemark = placemarks?.first {
let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate
print("Lat: \(coordinates.latitude) -- Long: \(coordinates.longitude)")
}
})
} label: {
Text("Press For Location")
}
}
}
}
So I have a program that takes the user's input and turns it into a latitude and longitude coordinate. I want to use that coordinate but I don't really know how to call it anywhere in the program. Any help would be appreciated! The code is below :)
import SwiftUI
import MapKit
struct ContentView: View {
let geocoder = CLGeocoder()
@State private var address = ""
var body: some View {
VStack {
TextField("Enter Location", text: $address)
Button {
geocoder.geocodeAddressString(address, completionHandler: {(placemarks, error) -> Void in
if((error) != nil){
print("Error", error ?? "")
}
if let placemark = placemarks?.first {
let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate
print("Lat: \(coordinates.latitude) -- Long: \(coordinates.longitude)")
}
})
} label: {
Text("Press For Location")
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:您可以掩盖LAT,并长时间到真实位置。
Edited: you can covert the lat and long value to real location.