如何在程序中的其他地方获取可变输出|斯威夫特

发布于 2025-02-08 09:27:35 字数 1003 浏览 1 评论 0原文

因此,我有一个程序,可以将用户的输入转换为纬度和经度坐标。我想使用该坐标,但我真的不知道如何在程序中的任何地方称呼它。任何帮助将不胜感激!代码在下面:)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三岁铭 2025-02-15 09:27:35

编辑:您可以掩盖LAT,并长时间到真实位置。

import MapKit
import SwiftUI

struct ContentView: View {

let geocoder = CLGeocoder()
@State private var result = "result of lat & long"
@State private var address = ""
@State private var lat = 0.0
@State private var long = 0.0
@State private var country = "country name"
@State private var state = "state name"
@State private var zip = "zip code"

var body: some View {
VStack {
    
    //Enter "Florida"
    TextField("Enter Location", text: $address)
    
    //Press button
    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)")
                //added code
                result = "Lat: \(coordinates.latitude) -- Long: \(coordinates.longitude)"
                lat = coordinates.latitude
                long = coordinates.longitude
            }
        })
    } label: {
        Text("Press For Location")
    }
    
    //result will show "Lat: 29.5449611 -- Long: -81.7627933"
    Text("\(result)")
    
    Spacer()
    
    //Press this button after you pressed the location button
    //it will convert the lat & long to real location
    Button {
        reverseLatLong(lat: lat, long: long)
    } label: {
        Text("Reverse to address")
    }
    Text("\(country)") //United States
    Text("\(state)") //FL
    Text("\(zip)") //32177
}

}
func reverseLatLong(lat:Double, long:Double){
    
    let geoCoder = CLGeocoder()
    let location = CLLocation(latitude: lat, longitude: long)
    
    geoCoder.reverseGeocodeLocation(location, completionHandler: { (sub, e) -> Void in
        
        var l: CLPlacemark!
        l = sub?[0]
        
        if let lcountry = l.country {
            country = lcountry
        }

        if let lstate = l.administrativeArea {
            state = lstate
        }
        
        if let lzip = l.postalCode {
            zip = lzip
        }
    })
}
}

Edited: you can covert the lat and long value to real location.

import MapKit
import SwiftUI

struct ContentView: View {

let geocoder = CLGeocoder()
@State private var result = "result of lat & long"
@State private var address = ""
@State private var lat = 0.0
@State private var long = 0.0
@State private var country = "country name"
@State private var state = "state name"
@State private var zip = "zip code"

var body: some View {
VStack {
    
    //Enter "Florida"
    TextField("Enter Location", text: $address)
    
    //Press button
    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)")
                //added code
                result = "Lat: \(coordinates.latitude) -- Long: \(coordinates.longitude)"
                lat = coordinates.latitude
                long = coordinates.longitude
            }
        })
    } label: {
        Text("Press For Location")
    }
    
    //result will show "Lat: 29.5449611 -- Long: -81.7627933"
    Text("\(result)")
    
    Spacer()
    
    //Press this button after you pressed the location button
    //it will convert the lat & long to real location
    Button {
        reverseLatLong(lat: lat, long: long)
    } label: {
        Text("Reverse to address")
    }
    Text("\(country)") //United States
    Text("\(state)") //FL
    Text("\(zip)") //32177
}

}
func reverseLatLong(lat:Double, long:Double){
    
    let geoCoder = CLGeocoder()
    let location = CLLocation(latitude: lat, longitude: long)
    
    geoCoder.reverseGeocodeLocation(location, completionHandler: { (sub, e) -> Void in
        
        var l: CLPlacemark!
        l = sub?[0]
        
        if let lcountry = l.country {
            country = lcountry
        }

        if let lstate = l.administrativeArea {
            state = lstate
        }
        
        if let lzip = l.postalCode {
            zip = lzip
        }
    })
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文