Convert MKAnnotation Coordinates to View Coordinates

梦想的初衷 提交于 2019-12-01 22:01:46

Use convertCoordinate:toPointToView:. Something like:

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

This is perfect, but there is one little problem. If you use a different image to your Pin, than you will need make a correction to Your view Appoint to center of your pin. Like this

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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!