NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Setting Map coords in generic view?

Forums > SwiftUI

Hi all, I have a generic detail view where I want to view a different location (lat/long) each time it is called. Is there a simple way to set the hardcoded CLLocationCoordinate2D coords when the view starts?

@State private var region = MKCoordinateRegion(
        center: CLLocationCoordinate2D(latitude: 37.334_900,
                                       longitude: -122.009_020),
        latitudinalMeters: 750,
        longitudinalMeters: 750

The map is not interactable / dragable, its just a fixed view showing a set location.


Is it randomly different or you want to pass in different specific coordinates each time?


Hi, its a detail view so I pass in a struct with long/lat each time. In the example above I want to replace "37.334" and "-122.009" with the values from that struct each time I display the detail view.


Create the MKCoordinateRegion in the initializer of your Detail View with the data of your struct?


You've marked your region variable as private. So, you can not pass in values to this variable from a parent or external view. Perhaps you should share more of your DetailView code and explain what you're trying to accomplish?

You've been a member of HWS+ for 20+ months, so explaining @State might be too basic for you?

Please show code with comments, and let us know what you're trying to accomplish.


Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

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

Reply to this topic…

You need to create an account or log in to reply.

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.