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

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.

2      

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

2      

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.

2      

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

2      

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.

2      

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.