3

我有一个带有方形叠加层的 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 滚动视图吗?还是有其他方法?

谢谢

沙尼

4

1 回答 1

0

经过几个小时的努力,我得到了这个对我来说非常有用的解决方案。

它混淆了:

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

这个链接:限制 mkmapview从@Anna Karenina 滚动到我的问题。

这是我的代码:

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

}
于 2012-06-22T21:06:08.683 回答