Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After updating GMUClusterItem, updated GMUClusterItem is gone from map & come again when zoomOut the map. #235

Closed
StellarKuldeep opened this issue Jul 27, 2019 · 2 comments
Labels

Comments

@StellarKuldeep
Copy link

StellarKuldeep commented Jul 27, 2019

I have load multiple pins in GoogleMap with clustering and below is my code.

func initializeClusterItems() {
        let iconGenerator = GMUDefaultClusterIconGenerator()
        let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
        let renderer = GMUDefaultClusterRenderer(mapView: mapView, clusterIconGenerator: iconGenerator)
        renderer.delegate = self
        clusterManager = GMUClusterManager(map: mapView, algorithm: algorithm, renderer: renderer)
}
    
func renderer(_ renderer: GMUClusterRenderer, willRenderMarker marker: GMSMarker) {
    marker.groundAnchor = CGPoint(x: 0.1, y: 0.45)
    if let markerData = (marker.userData as? POIItem) {
        let infoWindow = Bundle.main.loadNibNamed("InitialMapInfoView", owner: self, options: nil)?.first as! InitialMapInfoView
        infoWindow.imgUser.sd_setImage(with: URL(string: markerData.friend.user_details.user_photo_small), placeholderImage: #imageLiteral(resourceName: "User_profile"), options: .highPriority, completed: nil)
            
        if !markerData.friend.user_details.isUserOnline {
            infoWindow.imgCar.image = UIImage.init(named: "small_inactive_" + markerData.friend.user_details.car_personality_name)
        }
        else {
            infoWindow.imgCar.image = UIImage.init(named: "small_" + markerData.friend.user_details.car_personality_name)
        }
            
        if let objCurrentMarker = SharedData.sharedInstance.allFriends.first(where: {$0.user_details.user_id == markerData.friend.user_details.user_id}) {
                
            markerData.friend.user_details.latitude = objCurrentMarker.user_details.latitude
            markerData.friend.user_details.longitude = objCurrentMarker.user_details.longitude
                
            if !objCurrentMarker.user_details.isUserOnline {
                infoWindow.imgCar.image = UIImage.init(named: "small_inactive_" + objCurrentMarker.user_details.car_personality_name)
            }
            else {
                infoWindow.imgCar.image = UIImage.init(named: "small_" + objCurrentMarker.user_details.car_personality_name)
            }
                
            CATransaction.begin()
            CATransaction.setAnimationDuration(1.0)
            marker.position = CLLocationCoordinate2D.init(latitude: objCurrentMarker.user_details.latitude, longitude: objCurrentMarker.user_details.longitude)
            CATransaction.commit()
        }
        else {
            // item could not be found
        }
            
        infoWindow.lblName.text = markerData.friend.user_details.name
        infoWindow.btnImgVW.tag = markerData.userIndex
        infoWindow.btnImgVW.addTarget(self, action: #selector(btnUserTapped(_:)), for: .touchUpInside)
        marker.accessibilityHint = String(markerData.userIndex)
        marker.iconView = infoWindow
        marker.tracksViewChanges = false
    }
}

func setMarkers() {
    arrMarkers.removeAll()
    for i in 0..<SharedData.sharedInstance.allFriends.count {
        let marker = MyMarker()
        let friend = SharedData.sharedInstance.allFriends[i]
        marker.id = friend.user_details.user_id
        marker.position = CLLocationCoordinate2D.init(latitude: friend.user_details.latitude , longitude: friend.user_details.longitude)
        marker.accessibilityHint = String(i)
            
        marker.icon = #imageLiteral(resourceName: "trans")
        marker.tracksViewChanges = true
        marker.map = mapView
            
        arrMarkers.append(marker)
        self.generatePOIItems(String(format: "%d", i), position: marker.position, icon: nil, friend: friend, userIndex: i)
    }
    clusterManager.cluster()
    clusterManager.setDelegate(self, mapDelegate: self)
}

func generatePOIItems(_ accessibilityLabel: String, position: CLLocationCoordinate2D, icon: UIImage?, friend: WallBeeppClass, userIndex: Int) {
    let name = "Item \(accessibilityLabel)"
    let item = POIItem(position: CLLocationCoordinate2DMake(position.latitude, position.longitude), name: name, friend: friend, userIndex: userIndex)
    clusterManager.add(item)
}

class POIItem: NSObject, GMUClusterItem {
    var position: CLLocationCoordinate2D
    var name: String!
    var friend: WallBeeppClass!
    var userIndex: Int!
    
    init(position: CLLocationCoordinate2D, name: String, friend: WallBeeppClass, userIndex: Int) {
        self.position = position
        self.name = name
        self.friend = friend
        self.userIndex = userIndex
    }
}

I want to update the users latitude & longitude inside clusterManagerItems.

I tried the below line to change location data inside func renderer(_ renderer: GMUClusterRenderer, willRenderMarker marker: GMSMarker){} but app crashing with this error : 'All items should be mapped to a distance'

markerData.position = CLLocationCoordinate2D.init(latitude: objCurrentMarker.user_details.latitude, longitude: objCurrentMarker.user_details.longitude)

If I add below line then it works but when I zoomIn/ZoomOut the map. clusterManager item load at old position and then it load to new position.

marker.position = CLLocationCoordinate2D.init(latitude: objCurrentMarker.user_details.latitude, longitude: objCurrentMarker.user_details.longitude)

EDIT 1

I'm able to update the already added GMUClusterItem but the issue is that when i call clusterManager.cluster() again when I receive new data from server then GMUClusterItem is gone and come again on map.

Please guide me how to update already added Clustermanager item?

Thanks in advance

@StellarKuldeep StellarKuldeep changed the title How to update existing GMUClusterItem in GoogleMap? After updating GMUClusterItem, updated GMUClusterItem is gone from map & come again when zoomOut the map. Jul 29, 2019
@stale
Copy link

stale bot commented Oct 27, 2019

This issue has been automatically marked as stale because it has not had recent activity. Please comment here if it is still valid so that we can reprioritize. Thank you!

@stale stale bot added the stale label Oct 27, 2019
@stale
Copy link

stale bot commented Nov 26, 2019

Closing this. Please reopen if you believe it should be addressed. Thank you for your contribution.

@stale stale bot closed this as completed Nov 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant