2012-06-22 62 views
3

我有一个的MKMapView被描述为一个正方形叠加:的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的滚动视图吗?或者还有其他方法?

感谢

沙尼

+0

尝试这样的回答:http://stackoverflow.com/questions/4119117/restrict-mkmapview-scrolling – Anna

回答

0

几个小时敲打我的头后,我得到了这个解决方案,为我工作的伟大。

它混淆了:

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

而这个链接:restrict mkmapview scrolling从@Anna卡列尼娜评论我的问题。

,这是我的代码:

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

} 
+1

请添加使用这个所有代码。 MKMapView类别,您使用的接口。我不明白它是如何工作的[[mapView zoomLevel]' – rowwingman

+1

是的,如果你要添加你的代码,请包含所有内容。 – coolcool1994

+0

同意rowwingman。你可以告诉这个[mapView zoomLevel]从哪里来? – Xcoder