iOS如何在点击后将MKMapView居中放置在MKAnnotationView详图索引附件上?
如何在MKAnnotationView的详图索引附件上居中放置地图视图?我知道如何在实际的annotationView上居中放置地图,但我不知道如何在annotationView的详图索引附件上居中放置地图。有什么想法吗iOS如何在点击后将MKMapView居中放置在MKAnnotationView详图索引附件上?,ios,mkmapview,mapkit,Ios,Mkmapview,Mapkit,如何在MKAnnotationView的详图索引附件上居中放置地图视图?我知道如何在实际的annotationView上居中放置地图,但我不知道如何在annotationView的详图索引附件上居中放置地图。有什么想法吗 // This centers the map on the MKAnnotationView. However, I need to be able to // center the map on the MKAnnotationView's 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];
}
//这将使地图在MKAnnotationView上居中。然而,我需要能够
//将地图居中放置在MKAnnotationView的详图索引附件上。
-(无效)地图视图:(MKMapView*)地图视图注释视图:(MKAnnotationView*)视图调用访问控制点击:(UIControl*)控制
{
id注释=view.annotation;
[self.mapView setCenterCoordinate:[注释坐标]已设置动画:是];
}
您可以使用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];
}
你,我的朋友,是一个传奇和老板。非常感谢。您发布的代码使我能够在点击的实际UIControl上将地图居中。为了使您的答案符合我的要求,我只需将所有对
control
的引用替换为control.superview
。在你有来协调romview:control.superview的地方,我一字不差地保留了那一行,不需要翻译。通过这些小的编辑,我现在能够在地图上集中整个accessoryView,以响应用户点击accessoryView的控件。好极了!!