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

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.

   

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, 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!

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.