iOS如何在点击后将MKMapView放在MKAnnotationView标注附件上?

时间:2014-05-21 23:08:26

标签: ios mkmapview mapkit

如何将mapView集中在MKAnnotationView的calloutAccessory上?我知道如何将地图置于实际的annotationView中心,但我不知道如何将地图置于annotationView的calloutAccessory中心。有什么想法吗?

// 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];
}

1 个答案:

答案 0 :(得分:7)

您可以使用MKMapView方法convertPoint: toCoordinateFromView:方法将注释视图的中心转换为坐标,然后将地图置于该坐标的中心位置 -

-(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];

}