如何根据所选的注释移动MKMapView [英] How to move MKMapView based on selected annotation

查看:198
本文介绍了如何根据所选的注释移动MKMapView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MKMapView填充我的视图的整体,但当一个引脚被选中,我向上滑动另一个视图在地图上。我想移动地图,以便图钉将显示在地图可见区域的中心。

I have an MKMapView which populates the entirety of my view, but when a pin is selected, I am sliding up another view on top of the map. I want to move the map so the pin will appear in the centre of the visible area of the map.

很难解释,但希望它有意义!
提前感谢。

Difficult to explain but hopefully it makes sense! Thanks in advance.

推荐答案

您可以尝试从 visibleMapRect 为地图视图,将注释的坐标转换为MKMapPoint,重置MKMapRect的原点,使MKMapPoint在适当的位置,然后使用 setVisibleMapRect:animated:将可见区域设置为新的MKMapRect。

You could try getting the MKMapRect from visibleMapRect for the map view, converting the annotation's coordinate to an MKMapPoint, resetting the MKMapRect's origin so the MKMapPoint is in the appropriate position, and then using setVisibleMapRect:animated: to set the visible region to the new MKMapRect.

例如,如果要移动地图,使注释水平居中,垂直居中25%你可以这样做:

For example, if you wanted to move the map so the annotation is centered horizontally and 25% of the way down vertically, you could do something like this:

MKMapRect r = [mapView visibleMapRect];
MKMapPoint pt = MKMapPointForCoordinate([annotation coordinate]);
r.origin.x = pt.x - r.size.width * 0.5;
r.origin.y = pt.y - r.size.height * 0.25;
[mapView setVisibleMapRect:r animated:YES]; 

这篇关于如何根据所选的注释移动MKMapView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆