MKMapView - 限制地图滚动到方形叠加

时间:2012-06-22 07:54:42

标签: ios mkmapview mapkit mkoverlay

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

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

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

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

由于

沙尼

1 个答案:

答案 0 :(得分:0)

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

混淆了:

这篇文章:set the zoom level of an mkmapview

这个链接:来自@Anna Karenina的restrict mkmapview scrolling评论我的问题。

这是我的代码:

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

}