设置MapView的中心坐标不考虑MapView的完整大小(屏幕外部分) [英] Setting a MapView's center coordinate does not take into account full size of MapView (off-screen portion)

查看:322
本文介绍了设置MapView的中心坐标不考虑MapView的完整大小(屏幕外部分)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我有一个包含全屏MKMapView的视图。 MapView的下半部分覆盖了一个具有半透明单元格的TableView,以便显示地图。

I have a view that includes a full-screen MKMapView. The bottom half of the MapView is covered with a TableView that has translucent cells so that the map shows through.

当我以注释坐标为中心时,当前位置,例如注释视图被TableView阻止。

When I center on an annotation coordinate, the current location for example, the annotation view is blocked by the TableView.

- (void)centerOnCurrentLocation
{
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.locationManager.location.coordinate, 50000, 50000);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}

尝试解决方案

我首先尝试以编程方式移动地图以偏移中心,向上移动注释。如果我关闭动画,这种方法很有效,尽管这不是一个简单的解决方案。我也不想关闭动画。

I first tried to programmatically shift the map to offset the center, shifting the annotation up. This approach works if I turn off animation, though it's not a simple solution. Nor do I want to turn off animation.

相反,我认为如果我将MapView从屏幕顶部扩展到与TableView相同的高度MapView正是我想要的地方。它不起作用!当我在较大的MapView上设置中心坐标时,注释仍然出现在屏幕的中心,而不是在较大的MapView的中心。在计算其中心时,似乎MKMapView正在使用视图的可见区域。

Instead I thought that if I extend the MapView off the top of the screen by the same height as the TableView the center of the MapView would be exactly where I want it. It doesn't work! When I set the center coordinate on the larger MapView the annotation still appears in the center of the screen, not in the center of the larger MapView. It seems like MKMapView is using the view's visible region when calculating its center.

当我使用放大的MapView在当前位置居中时,我希望看到这一点。

This is what I would expect to see when I center on current location using the enlarged MapView.

如果只有MKMapView有一个centerOffset属性允许你将中心移动一些CGPoint。

If only MKMapView had a centerOffset property that allowed you to shift the center by some CGPoint.

有没有人有一个简单的,优雅的解决方案来抵消MapView的中心?

Does anybody have a simple, elegant solution to offset a MapView's center?

推荐答案

为了澄清我的评论,你可以使用 setVisibleMapRect:edgePadding:创建所需的偏移量。您需要将它放在mapView委托方法 regionDidChangeAnimated:这将在地图更改地图时随时触发,因此您需要使用标志包装代码以了解何时应用偏移量或可以多次应用。以下是您需要做的事情:

Just to clarify on my comment, you can use setVisibleMapRect: edgePadding: to create the offset you want. You will need to put it in the mapView delegate method regionDidChangeAnimated: this will fire anytime the region changes on your map so you need to wrap your code with a flag to know when to apply the offset or it can be applied multiple times. Here is what you need to do:

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{

    if (!self.mapInsetsSet) {
        self.mapInsetsSet=TRUE;
        [self.myMapView setVisibleMapRect:self.myMapView.visibleMapRect edgePadding:UIEdgeInsetsMake(10, 0, 0, 0) animated:YES];
    }

}

在上面的代码中, mapInsetsSet 只是一个bool,您可以在其他位置设置以控制何时应用偏移量。此外,您需要将 UIEdgeInsetsMake 调整为所需的偏移量。

In the above code, mapInsetsSet is just a bool that you can set elsewhere to control when to apply the offset. Also, you will need to adjust the UIEdgeInsetsMake to the desired offset.

这篇关于设置MapView的中心坐标不考虑MapView的完整大小(屏幕外部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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