0

So I'm trying to make an iOS app using swiftui that forward geocodes an address and then places those coordinates in variables that can be used in the rest of my views. The function I'm using for forward geocoding looks like this:

import SwiftUI
import CoreLocation

struct ContentView: View {
    @State var myText = "Some Text just for reference"
    @State var location: CLLocationCoordinate2D?
    @State var lat: Double?
    @State var long: Double?

    var body: some View {
        VStack{
        Text(myText)
                  .onAppear {
                      self.getLocation(from: "22 Sunset Ave, East Quogue, NY") { coordinates in
                          print(coordinates ?? 0) // Print here
                          self.location = coordinates // Assign to a local variable for further processing
                        self.long = coordinates?.longitude
                        self.lat = coordinates?.latitude
                      }
              }
         Text("\(long)")
         Text("\(lat)")
        }

    }
      func getLocation(from address: String, completion: @escaping (_ location: CLLocationCoordinate2D?)-> Void) {
          let geocoder = CLGeocoder()
          geocoder.geocodeAddressString(address) { (placemarks, error) in
              guard let placemarks = placemarks,
          let location = placemarks.first?.location?.coordinate else {
              completion(nil)
              return
        }
          completion(location)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

My questions are, how should I be calling this function inside of my contentview file so I can save the coordinates as variables and how do I call the function to print them to the screen just to verify the code is operating correctly.

1 Answer 1

1

You can do it like this within the onAppear() method in a very simple manner. However, I would encourage you to use a View Model to fetch the coordinates using an address.

struct ContentView: View {
    @State var myText = "Some Text just for reference"
    @State var location: CLLocationCoordinate2D?
    var body: some View {
        Text(myText)
            .onAppear {
                self.getLocation(from: "Some Address") { coordinates in
                    print(coordinates) // Print here 
                    self.location = coordinates // Assign to a local variable for further processing
                }
        }
    }

    func getLocation(from address: String, completion: @escaping (_ location: CLLocationCoordinate2D?)-> Void) {
        let geocoder = CLGeocoder()
        geocoder.geocodeAddressString(address) { (placemarks, error) in
            guard let placemarks = placemarks,
            let location = placemarks.first?.location?.coordinate else {
                completion(nil)
                return
            }
            completion(location)
        }
    }
}
Sign up to request clarification or add additional context in comments.

14 Comments

When Implementing this approach the following error is given: Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type. Also the warning: Result of call to 'onAppear(perform:)' is unused, is display. Any suggestions on how to fix these?
@TJD'Alessandro are you using the onAppear like I did in my example?
I copied this code into a brand new project and when I ran it, the errors I mentioned appeared
@TJD'Alessandro can you share your code here in the chat? chat.stackoverflow.com/rooms/212370/swiftui-geocoding
I am not able to post in the chat as I have less than 20 reputation. The code in my project is exactly what you wrote in your answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.