Home > Enterprise >  How To get variable output somewhere else in the program | SwiftUI
How To get variable output somewhere else in the program | SwiftUI

Time:06-20

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")
            }
        }

    }
}

CodePudding user response:

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
        }
    })
}
}
  • Related