MKMapView - 限制地图滚动到方形叠加 [英] MKMapView - Limit map scroll to a square overlay

查看:217
本文介绍了MKMapView - 限制地图滚动到方形叠加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有方形叠加的MKMapView,描述如下:

I have a MKMapView with a square overlay described as:

   CLLocationCoordinate2D coordsBg[5]={
    CLLocationCoordinate2DMake(31.750865,35.180882),
    CLLocationCoordinate2DMake(31.740331,35.180882),
    CLLocationCoordinate2DMake(31.740331,35.165452),
    CLLocationCoordinate2DMake(31.750865,35.165452),
    CLLocationCoordinate2DMake(31.750865,35.180882)
};     

MKPolygon *bg=[MKPolygon polygonWithCoordinates:coordsBg count:5];
[map addOverlay:bg];

我希望限制用户滚动到叠加层之外。

I wish to limit the user from scrolling outside of the overlay.

我可以限制MKMapView滚动视图吗?还是有其他方法?

Can I limit the MKMapView scroll view for that? Or there is an other method?

谢谢

Shani

推荐答案

经过几个小时的敲击,我得到了这个对我有用的解决方案。

After Few hours of banging my head, I got to this solution that work great for me.

它混合了up:

这篇文章:设置mkmapview的缩放级别

此链接:限制mkmapview滚动来自@Anna Karenina评论我的问题。

And This link: restrict mkmapview scrolling from @Anna Karenina comment to my question.

这是我的代码:

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
    lastGoodMapRect = mapView.visibleMapRect;
    lastGoodRegion = mapView.region;
}

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

    if (manuallyChangingMapRect) {
        manuallyChangingMapRect=NO;
        return;
    }

    if( [mapView zoomLevel] > 16 )
    {
        [mapView setCenterCoordinate:lastGoodRegion.center zoomLevel:16 animated:YES];
    }

    MKMapRect visibleRect = mapView.visibleMapRect;
    MKMapRect OverlayRect = bg.boundingMapRect;
    MKMapRect intersectionRect = MKMapRectIntersection(visibleRect,OverlayRect);

    //you can change the min and max zoom off course
    if(!MKMapRectEqualToRect(visibleRect,intersectionRect)){
        if( [mapView zoomLevel] < 15){

            [mapView setCenterCoordinate:lastGoodRegion.center zoomLevel:15 animated:YES];
        }else if( [mapView zoomLevel] > 16 )
        {
            [mapView setCenterCoordinate:lastGoodRegion.center zoomLevel:16 animated:YES];
        }else{
            manuallyChangingMapRect=YES;
            [mapView setVisibleMapRect:lastGoodMapRect animated:YES];
        }
    }

}

这篇关于MKMapView - 限制地图滚动到方形叠加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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