Skip to main content

Swift Google Map reuse/cache marker

Custom marker seem quite slow.

Caching and reusing marker on update / refreshing data that existing in current marker list.

       // Caching/reusing spot
        // TODO order by creation time
       
        // List old spots that not reusable
        var oldSpotIndexes = self.mNearSpotMyMapList.map { $0.shopId }
        var newSpotIndexes = nearSpotMyMapList.map { $0.shopId }
        print(newSpotIndexes)

        let reusableOldSpotIndexes = newSpotIndexes.filter{ oldSpotIndexes.contains($0) }
        print(reusableOldSpotIndexes)
       
        // Exclude reusable spot in new fetched Spots data
        newSpotIndexes = Array( Set(newSpotIndexes).subtracting(Set(reusableOldSpotIndexes)) )
       
        let unusedSpotIndexes = Set(oldSpotIndexes).subtracting(Set(reusableOldSpotIndexes))
        // list index/id/shopID ... of marker to remove
       
        print(oldSpotIndexes)
        self.mNearSpotMyMapList = self.mNearSpotMyMapList.filter { reusableOldSpotIndexes.contains($0.shopId) }
        onlyNewNearSpotList = onlyNewNearSpotList.filter { newSpotIndexes.contains($0.shopId) }

       
        if(onlyNewNearSpotList.count > 0) {
            self.mNearSpotMyMapList.append(contentsOf: onlyNewNearSpotList)
            print("††† \(self.mNearSpotMyMapList.count)")
        }
       
        // Update marker
        self.updateMarker2()


Update marker:

self.myMapView.clear()
self.mMarkers.remove(at: Dictionary<String, GMSMarker>.Index)

Comments