Skip to content
This repository has been archived by the owner on Sep 13, 2021. It is now read-only.

Slecting Annotation View? ::question:: #23

Closed
MMasterson opened this issue Nov 9, 2015 · 4 comments
Closed

Slecting Annotation View? ::question:: #23

MMasterson opened this issue Nov 9, 2015 · 4 comments

Comments

@MMasterson
Copy link

How is the select of the annotation fired? I'm trying to snap to a region and display the annotation.

@MMasterson MMasterson changed the title Slecting Annotation View? Slecting Annotation View? ::question:: Nov 9, 2015
@filipbec
Copy link
Contributor

Hi @MMasterson,

you can do something like this:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    if ([view.annotation isKindOfClass:[FBAnnotationCluster class]]) {
        FBAnnotationCluster *cluster = (FBAnnotationCluster *)view.annotation;

        int i = 0;
        MKMapPoint points[cluster.annotations.count];

        for (id<MKAnnotation> annotation in cluster.annotations){
            points[i++] = MKMapPointForCoordinate(annotation.coordinate);
        }

        MKPolygon *polygon = [MKPolygon polygonWithPoints:points count:i];
        MKMapRect newMapRect = [polygon boundingMapRect];

        [mapView setVisibleMapRect:newMapRect edgePadding:UIEdgeInsetsMake(50, 50, 50, 50) animated:YES];
    }
}

@Quentin-Malgaud
Copy link

hi @filipbec , thank you very much for all your work.
I encountered the same problem as @MMasterson but I am working in swift. I am trying to translate the code above but with no great success. I don't really understand what's going on with the MKPolygon. Could you give me some hints? I could then upload the translated version here ;)

Thank you

@MMasterson
Copy link
Author

@Quentin-Malgaud does this help?

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
    if (view.annotation is FBAnnotationCluster) {
        var cluster: FBAnnotationCluster = view.annotation as! FBAnnotationCluster
        var i: Int = 0
        var points: MKMapPoint
        for annotation: MKAnnotation in cluster.annotations {
            points[i++] = MKMapPointForCoordinate(annotation.coordinate)
        }
        var polygon: MKPolygon = MKPolygon.polygonWithPoints(points, count: i)
        var newMapRect: MKMapRect = polygon.boundingMapRect()
        mapView.setVisibleMapRect(newMapRect, edgePadding: UIEdgeInsetsMake(50, 50, 50, 50), animated: true)
    }
}

*Points is the list of the pins you wanna show annotations for
This example showa all annotations in the array cluster.annotations.
the i keeps track of the number of annotations, so when it loops through it will still show all of them.

@Quentin-Malgaud
Copy link

Hi @MMasterson, thank you very much for this one. Helped me a lot ;)
Still had to change a word here and there, if you want to edit your post:

   func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
    if view.annotation is FBAnnotationCluster {
        let cluster: FBAnnotationCluster = view.annotation as! FBAnnotationCluster
        var i: Int = 0
        var points: [MKMapPoint] = []

        for annotation: MKAnnotation in cluster.annotations {
            points.append(MKMapPointForCoordinate(annotation.coordinate))
            i++
        }

        let polygon: MKPolygon = MKPolygon(points: &points, count: i)
        let newMapRect: MKMapRect = polygon.boundingMapRect
        mapView.setVisibleMapRect(newMapRect, edgePadding: UIEdgeInsetsMake(50, 50, 50, 50), animated: true)
    }
}

Thank you again

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

No branches or pull requests

3 participants