0

I am passing an argument locationPins into MyMapView as a Binding and then again pass locationPins to MapView as another Binding. But when I pass it to MapView as an argument I get error: "Argument passed to call that takes no arguments"

struct MyMapView: View {
@Binding var locationPins: [GpsData.MyEndPt]

var body: some View {
    TabView {
        NavigationView {
            ZStack {
                VStack (alignment: .leading){
                    MapView(locationPins: locationPins) // <<----- ERROR HERE
                        .ignoresSafeArea()
                        
                }
            }
            .safeAreaInset(edge: .bottom) {
              Color.clear
                .frame(height: 0)
                .background(.white)
            }

        }
    }
    
}

struct MapView: UIViewRepresentable {
    @Binding var locationPins: [GpsData.MyEndPt]
    @State var mylocationPins: [GpsData.MyEndPt] = []
    
    init() {
        self.mylocationPins = toPins(locationPins: self.locationPins)
    }

    func toPins(locationPins: [GpsData.MyEndPt]) -> [GpsData.MyEndPt] {
        
        for location in locationPins {
         // use locationPins to populate myLocationPins and return
        }
        return mylocationPins
    }

}

If I remove the Binding definition of locationPins inside MapView, then I cannot pass the value.

How to fix this?

4
  • You included a lot of unnecessary code! Please see how to create a minimal, reproducible example.
    – Stoic
    Commented Feb 23 at 20:48
  • You're declaring init() with no parameters. If you want to initialize with parameters, you must declare them in the initializer's signature
    – jnpdx
    Commented Feb 23 at 21:01
  • 1
    But, you've got a pretty funny architecture using init to set a @State variable based on what's in a Binding. I would strongly urge refactoring this to use the typical makeUIView and updateNSView pattern and not do anything custom in init. Then, just remove your init and let the compiler synthesize the initializer and you'll be fine
    – jnpdx
    Commented Feb 23 at 21:02
  • Yes. As suggested, I removed the init and passed it directly to MkMapViewDeleagte and it worked. When I display the MapView, three markers come up but it zooms in on one marker. I have to zoom out to see all of them. How can I make the map expand enough and fit all three markers in display
    – vrao
    Commented Feb 23 at 22:49

0

Browse other questions tagged or ask your own question.