为什么 iOS 上没有显示 GMUClusterManager?

Why GMUClusterManager is not showing on iOS?

我尝试使用 UIViewRepresentable 模式来使用 SwiftUI 视图,GoogleMaps 背景有效但 ClusterManager 无效 展示任何东西。有什么问题吗?

我尝试生成 10000 个标记,就像 Google Maps documentation, but instead of using UIKit with "UIViewController" I try to use SwiftUI with UIViewRepresentable just like they did there 的样本一样。如果我只使用标记,它会起作用,但如果我尝试使用 ClusterManager,它就不起作用。

import SwiftUI
import GoogleMaps
import GoogleMapsUtils

struct GoogleMapsView: UIViewRepresentable {
    let kClusterItemCount = 10000
    let kCameraLatitude = 48.860294
    let kCameraLongitude = 2.338629

    func makeUIView(context: Self.Context) -> GMSMapView {
        print("-- GoogleMapsView -- : Making UiView MapView")
        
        let camera = GMSCameraPosition.camera(withLatitude: kCameraLatitude,
                                              longitude: kCameraLongitude, zoom: 10)
        let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        return mapView
    }
    func makeCoordinator() -> MapCoordinator {
        print("-- GoogleMapsView -- : Making Coordinator")
        
        return MapCoordinator(owner: self)
    }
    func updateUIView(_ mapView: GMSMapView, context: Context) {
        print("-- GoogleMapsView -- : Updating UI View")
        let iconGenerator = GMUDefaultClusterIconGenerator()
        let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
        let renderer = GMUDefaultClusterRenderer(mapView: mapView, clusterIconGenerator: iconGenerator)
        let clusterManager = GMUClusterManager(map: mapView, algorithm: algorithm, renderer: renderer)
        generateClusterItems(clusterManager: clusterManager)
        clusterManager.cluster()
    }
    private func generateClusterItems(clusterManager : GMUClusterManager) {
        let extent = 0.2
        for _ in 1...kClusterItemCount {
            let lat = kCameraLatitude + extent * randomScale()
            let lng = kCameraLongitude + extent * randomScale()
            let position = CLLocationCoordinate2D(latitude: lat, longitude: lng)
            let marker = GMSMarker(position: position)
            clusterManager.add(marker)
        }
    }
    /// Returns a random value between -1.0 and 1.0.
    private func randomScale() -> Double {
        return Double(arc4random()) / Double(UINT32_MAX) * 2.0 - 1.0
    }
}

我刚收到这个。

您正在 updateUIView 中创建 clusterManager 但没有将其保存在任何地方,因此它会立即被丢弃。在 Google 示例代码中,他们将 clusterManager 保存为视图控制器的 属性;你没有视图控制器,所以我建议将 clusterManager 和相关代码移动到你的 MapCoordinator class 中。协调器在您的视图的整个生命周期内保持不变,可以使用 context.coordinator.

makeUIViewupdateUIView 访问