Search code examples
iosswiftgoogle-mapsgoogle-maps-sdk-ios

Multiple GMSMarker show/hide consumes time


I have about 4000 markers on the GoogleMap and I am not using clustering. So, at the moment what I do to show/ hide markers on map is below.

marker.map = nil 
marker.map = mapView

There are some cases where I need to filter out some markers and make them visible else should be hidden. So I can achieve it with about code but with a cost to time.

markers.forEach { (marker) in
                if !arrDeviceID.contains(marker.deviceID ?? "") {
                        marker.map = nil
                } else {
                        marker.map = self.googleMapView
                }
            }

Same thing in Android there is a property Visibility which is working perfect and fast. But in iOS there is no such property.

So what can I do to improve performance.


Solution

  • lets say this is your markers array

    var markers = [GMSMarker]()
    

    prepare dictionary of markers, lets assume your device id is in Int

    func prepareMarkersDict(markers:[GMSMarker]) -> [Int:GMSMarker]{
        var markerDict = [Int:GMSMarker]()
        for marker in markers{
            markerDict[marker. deviceID] = marker
        }
        return markerDict
    }
    

    your array of device id which you wants to hide

    var arrDeviceIDToHide = [Int]()
    

    now iterate it and hide the marker

    let markerDict = prepareMarkersDict(markers:markers)
    for deviceId in arrDeviceIDToHide{
       let marker = markerDict[deviceId]
       marker?.map = nil
    }