John Erck
John Erck

Reputation: 9538

iOS how to center MKMapView on MKAnnotationView callout accessory after tap?

How would one go about centering the mapView on a MKAnnotationView's calloutAccessory? I know how to center the map on the actual annotationView, but I'm at a loss as to how to center the map on the annotationView's calloutAccessory. Any ideas?

// This centers the map on the MKAnnotationView. However, I need to be able to
// center the map on the MKAnnotationView's calloutAccessory.
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    id<MKAnnotation> annotation = view.annotation;
    [self.mapView setCenterCoordinate:[annotation coordinate] animated:YES];
}

Upvotes: 2

Views: 1679

Answers (1)

Paulw11
Paulw11

Reputation: 115002

You can use the MKMapView method convertPoint: toCoordinateFromView: method to convert the centre of the annotation view to a coordinate and then center the map on that coordinate -

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{

    CGPoint annotationCenter=CGPointMake(control.frame.origin.x+(control.frame.size.width/2),control.frame.origin.y+(control.frame.size.height/2));

    CLLocationCoordinate2D newCenter=[mapView convertPoint:annotationCenter toCoordinateFromView:control.superview];
    [mapView setCenterCoordinate:newCenter animated:YES];

}

Upvotes: 7

Related Questions