2
votes

I have been testing out using MapBox for my SwiftUI application but I've run into issues where my annotations are not appearing on my MapView even though they appear to have been added. My code for my updateAnnotations command, which is called on updateUIView is:

    private func updateAnnotations() {
        if let currentAnnotations = mapView.annotations {
            mapView.removeAnnotations(currentAnnotations)
        }
        
        for marker in devices.positions {
            let annotation = MGLPointAnnotation(title: marker.name, coordinate: marker.coordinate)
            mapView.addAnnotation(annotation)
            print("ADDED \(annotation)")
            print(mapView.annotations)
        }
    }

devices is a @ObservedObject containing a positions array of points with a title and coordinate to plot. My output from this code is:

ADDED <MGLPointAnnotation: 0x285abc9f0; title = "Test"; subtitle = (null); coordinate = -36.892800, 174.625000>
Optional([<MGLPointAnnotation: 0x2866b7c60; title = "Test"; subtitle = (null); coordinate = -36.892800, 174.625000>])

This doesn't make sense to me - the output suggests that the annotation has been created but it doesn't show up on the map. Any help would be greatly appreciated.

1

1 Answers

1
votes

You need to pass in the MapView when using SwiftUI - e.g. change updateAnnotations() to updateAnnotations(_ mapView: MGLMapView)