UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: SwiftUI Map for iOS14

Forums > SwiftUI

// ios 14 only
struct NewMapView: View {

    @Binding var lat : Double
    @Binding var lon : Double

    @State var region = MKCoordinateRegion(center: CLLocationCoordinate2D(
    //latitude: lat, longitude: lon),
    latitude: 53.8862, longitude: 8.6706),
    latitudinalMeters: 250, longitudinalMeters: 250 )

    private func setRegio(){
        region = MKCoordinateRegion(center: CLLocationCoordinate2D(
                latitude: lat, longitude: lon),
                latitudinalMeters: 250, longitudinalMeters: 250)
    }

    var body: some View {
        if #available(iOS 14.0, *) {
           Map(coordinateRegion: $region)
                .onAppear {self.setRegio()}
        } else {
            EntireMapView(lat: $lat, lon: $lon) // Fallback on earlier versions
        }
    }
}

Hi,

I hope someone can help me. How can I bind my two variables lat and lon to @State var region? .onAppear is the first approach, but indeed too late. Thanks a lot for your support. With regards Peter from Germany

3      

Hey Peter,

Here's a solution that will work in SwiftUI 2.0. It's slightly verbose, but it works reliably and well:

struct MapView: View {
    @Binding private var lat: Double
    @Binding private var lon: Double

    private let initialLatitudinalMetres: Double = 250
    private let initialLongitudinalMetres: Double = 250

    @State private var span: MKCoordinateSpan?

    init(lat: Binding<Double>, lon: Binding<Double>) {
        _lat = lat
        _lon = lon
    }

    private var region: Binding<MKCoordinateRegion> {
        Binding {
            let centre = CLLocationCoordinate2D(latitude: lat, longitude: lon)

            if let span = span {
                return MKCoordinateRegion(center: centre, span: span)
            } else {
                return MKCoordinateRegion(center: centre, latitudinalMeters: initialLatitudinalMetres, longitudinalMeters: initialLongitudinalMetres)
            }
        } set: { region in
            lat = region.center.latitude
            lon = region.center.longitude
            span = region.span
        }
    }

    var body: some View {
        Map(coordinateRegion: region)
    }
}

Then to use it:

struct ContentView: View {
    @State private var lat = 50.1018
    @State private var lon = 14.2632

    var body: some View {
        MapView(lat: $lat, lon: $lon)
    }
}

3      

Boy, that's so much effort for two small numbers that can't be wanted by Apple.

Nevertheless, thank you very much for the quick help.

peter

4      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.