3

当用户单击图钉(如 Zillow 应用程序)时,我正在显示自定义 UIView。现在的问题是我需要将视图放在实际引脚的正上方。MKAnnotationView 坐标系与地图相关。如何获取相对于 iPhone 屏幕的坐标,然后将视图放置在该坐标上。

- (void)mapView:(MKMapView *)mv didSelectAnnotationView:(MKAnnotationView *)view
{  
    // get view from storyboard 
    ZillowSearchResultAnnotation *annotation = (ZillowSearchResultAnnotation *) view.annotation;

    PropertyAbstractViewController *propertyAbstractViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PropertyAbstractIdentifier"];
    propertyAbstractViewController.view.frame = CGRectMake(0, 0, 100, 100);

    [propertyAbstractViewController bind:annotation.data];

    [mv addSubview:propertyAbstractViewController.view];

}
4

2 回答 2

12

使用convertCoordinate:toPointToView:。就像是:

UIView *view = ...
CGPoint p = [mv convertCoordinate:annotation.coordinate toPointToView:mv];
CGRect frame = CGRectMake(p.x,p.y,view.frame.size.width,view.frame.size.height);
view.frame = frame;
[self.view addSubView:view];
于 2012-07-10T21:22:35.050 回答
1

这是完美的,但有一个小问题。如果您对 Pin 图使用不同的图像,则需要对您的视图进行更正,将其指定到您的 Pin 图的中心。像这样

UIView *view = ...
CGPoint p = [mv convertCoordinate:annotation.coordinate toPointToView:mv];
CGRect frame = CGRectMake(p.x - (view.frame.size.width/2), 
                          p.y- (view.frame.size.height / 2),
                          view.frame.size.width,
                          view.frame.size.height);
view.frame = frame;
[self.view addSubView:view];
于 2014-09-15T13:19:23.097 回答