TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

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.