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

How to access MapView created in UIViewRepresentable from ViewModel in swiftui

Forums > SwiftUI

The only way I know of to create a Google/MapKit MapView in swiftui is to use UIViewRepresentable and embed in ContentView.

var body: some View {
                MapView()
                    .edgesIgnoringSafeArea(.all)
                }

The issue is how do you get access to the gmsMapView created in the UIViewRepresentable from a ViewModel? Access to gmsMapView is required to draw on the map....I asked this question on StackOverflow but got no response....link below. Thanks!

https://stackoverflow.com/questions/61463750/how-to-get-access-to-underlying-gmsmapview-from-uirepresentable-mapview

2      

Hi, sorry I do not have an answer to your question.. but I'm struggling with kind of the same thing.. where to put everything without it becomming a mess.. It feels more natural to place things in a ViewModel (@ObservableObject) than @Binding & @State in every view...

Is it ok to place the initialization of MKMapView and CLLocationManager in a ViewModel? or binding it to the ViewModel from the UIViewRepresentable (initialized there)?

I'm using these references maybe they can help you somehow:

https://kavsoft.tech/Swift/Uber%20Clone/

https://adrianhall.github.io/swift/2019/11/05/swiftui-location/

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.