首页 > 解决方案 > Mapbox MGLAnnotations 未在 MapView for Mapbox iOS 上呈现

问题描述

我一直在为我的 SwiftUI 应用程序使用 MapBox 进行测试,但我遇到了我的注释没有出现在我的 MapView 上的问题,即使它们似乎已经添加了。我在 updateUIView 上调用的 updateAnnotations 命令的代码是:

    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是一个 @ObservedObject ,其中包含一个positions点数组,其中包含要绘制的标题和坐标。我从这段代码的输出是:

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

这对我来说没有意义 - 输出表明注释已创建,但它没有显示在地图上。任何帮助将不胜感激。

标签: swiftmapboxmapbox-ios

解决方案


使用 SwiftUI 时需要传入 MapView - 例如更改updateAnnotations()updateAnnotations(_ mapView: MGLMapView)


推荐阅读