BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

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

1      

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

1      

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

2      

Save 50% in my Black Friday sale.

SAVE 50% To celebrate Black Friday, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.