[SOLVED] Why GMUClusterManager is not showing on iOS?

Issue

I try to use SwiftUI view using the UIViewRepresentable pattern, the GoogleMaps background works but the ClusterManager doesn’t
show anything. Is there something wrong?

I try to generate 10000 markers like in the sample of Google Maps documentation, but instead of using UIKit with "UIViewController" I try to use SwiftUI with UIViewRepresentable just like they did there. It works if I just use markers but if I try to use ClusterManager it doesn’t work.

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
    }
}

I just got this.
enter image description here

Solution

You are creating clusterManager inside updateUIView but not saving it anywhere, so it’s immediately discarded. In the Google sample code, they save clusterManager as a property of the view controller; you don’t have a view controller, so I’d recommend moving clusterManager and related code into your MapCoordinator class. Coordinators are kept around for the lifetime of your view and can be accessed from makeUIView and updateUIView using context.coordinator.

Answered By – Adam

Answer Checked By – Willingham (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.