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

Number of annotations inside a cluster change without animations #7

Open
ghost opened this issue May 10, 2017 · 2 comments
Open

Number of annotations inside a cluster change without animations #7

ghost opened this issue May 10, 2017 · 2 comments

Comments

@ghost
Copy link

ghost commented May 10, 2017

Hello !

So I've worked with this framework for swift with MKMapView, and I'm using a system where I show the number of annotations inside a cluster. The issue I've found is that if we have 2 nearby clusters and the algorithm recalculates such that one annotation moves from one cluster to the other, no animation will occur and the change is instant.

This issue isn't really apparent in the examples provided because it doesn't show how many annotations are inside a cluster, but if you use this code you can see it:

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        
        let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "annotation") ??
            newAnnotationView(forAnnotation: annotation)

        if let cluster = annotation as? CKCluster {
			let label = annotationView.viewWithTag(1235) as! UILabel
			
            if cluster.count > 1 {
                annotationView.canShowCallout = false
                annotationView.image = UIImage(named: "cluster")
				
				label.text = "\(cluster.count)"
				label.isHidden = false
			} else {
				label.isHidden = true
                annotationView.canShowCallout = true
                annotationView.image = UIImage(named: "marker")
            }
        }
        return annotationView;
    }
	
	private func newAnnotationView(forAnnotation annotation: MKAnnotation) -> MKAnnotationView {
		let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "annotation")
		let centerText = UILabel()
		centerText.translatesAutoresizingMaskIntoConstraints = false
		centerText.tag = 1235
		centerText.textColor = UIColor.cyan
		annotationView.addSubview(centerText)
		
		annotationView.addConstraint(NSLayoutConstraint(item: centerText, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: annotationView, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0))
		annotationView.addConstraint(NSLayoutConstraint(item: centerText, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: annotationView, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))
		
		return annotationView
	}

Find a location and zoom in slightly or zoom out slightly and you will notice that some clusters change the amount of annotations even though nothing goes in or out of them.

@maxep
Copy link
Contributor

maxep commented Jul 25, 2017

This is due to the conditions of animations:

  • [oldCluster containsAnnotation:newCluster.firstAnnotation] for a collapse.
  • [newCluster containsAnnotation:oldCluster.firstAnnotation] for an aggregation.

These conditions are simple in order to have good animation performances (even if it could be better). I could think of a more sophisticated conditions, like using a cluster bounds or an array intersection.

@ghost
Copy link
Author

ghost commented Jul 25, 2017

You don't need to replace it entirely, just give the option to choose between the two, like with the algorithms. I'm willing to sacrifice performance in certain scenarios.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant