当用户点击图钉时,我正在显示自定义UIView(如Zillow应用程序)。现在的问题是我需要将视图放在实际引脚的正上方。 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];
}
答案 0 :(得分: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];
答案 1 :(得分:1)
这很完美,但有一个小问题。如果您对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];