iOS-如何将MapView限制为特定区域?

| 我有以下问题: 我有一个“绘制的地图”(图像),我将其作为叠加层添加到MapView中。没问题..但是我需要将MapView限制为Overlay的区域,因此用户无法在该区域之外滚动/缩放。但是应该可以在\叠加层的“界限\”-表示我不能仅禁用MapView的缩放/滚动。 关于这个主题有什么想法/解决方案吗?使用MapView / -Kit的原因是我需要向自定义地图添加各种POI。仅使用ImageView + ScrollView呈现自定义地图时,这可能会变得更加复杂。 我对此主题进行了很多研究,但没有找到一个好的解决方案。 任何帮助表示赞赏! 最好的祝福, 基督教 编辑:这是我们的解决方案: 您提供左上和右下坐标以限制地图。 (最小)缩放级别也受到限制。我已停用减速功能,并且您可以从地图上稍微反弹一下(以获得更好的性能/辅助)。我在覆盖层上添加了约1公里的灰色边框,因此用户无法看到Google的原始世界地图。 LimitedMapView.h:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>


@interface LimitedMapView : MKMapView <UIScrollViewDelegate>{

}

@property (nonatomic, assign) CLLocationCoordinate2D topLeftCoordinate;
@property (nonatomic, assign) CLLocationCoordinate2D bottomRightCoordinate;


@end
LimitedMapView.m:
#import \"LimitedMapView.h\"

@implementation LimitedMapView

@synthesize topLeftCoordinate, bottomRightCoordinate;

- (void)scrollViewDidZoom:(UIScrollView *)scrollView{

    if([super respondsToSelector:@selector(scrollViewDidZoom:)]) [super scrollViewDidZoom:scrollView];

    if ([self region].span.latitudeDelta > 0.002401f || [self region].span.longitudeDelta > 0.003433f) {

        CLLocationCoordinate2D center = self.centerCoordinate;
        MKCoordinateSpan span = MKCoordinateSpanMake(0.002401f, 0.003433f);

        self.region = MKCoordinateRegionMake(center, span);

    }

}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

    if([super respondsToSelector:@selector(scrollViewDidEndDragging:)]) [super scrollViewDidEndDragging:scrollView willDecelerate:decelerate];

    MKCoordinateRegion currentRegion = self.region;
    bool changeRegionLong = YES;
    bool changeRegionLat = YES;

    // LONGITUDE    
    if((currentRegion.center.longitude - (currentRegion.span.longitudeDelta/2)) < self.topLeftCoordinate.longitude) {

        currentRegion.center.longitude = (topLeftCoordinate.longitude + (currentRegion.span.longitudeDelta/2));

    } else if((currentRegion.center.longitude + (currentRegion.span.longitudeDelta/2)) > self.bottomRightCoordinate.longitude) {

        currentRegion.center.longitude = (bottomRightCoordinate.longitude - (currentRegion.span.longitudeDelta/2));

    } else {

        changeRegionLong = NO;

    }

    // LATITUDE    
    if((currentRegion.center.latitude + (currentRegion.span.latitudeDelta/2)) > self.topLeftCoordinate.latitude) {

        currentRegion.center.latitude = (topLeftCoordinate.latitude - (currentRegion.span.latitudeDelta/2));

    } else if((currentRegion.center.latitude - (currentRegion.span.latitudeDelta/2)) < self.bottomRightCoordinate.latitude) {

        currentRegion.center.latitude = (bottomRightCoordinate.latitude + (currentRegion.span.latitudeDelta/2));

    } else {

        changeRegionLat = NO;

    }

    if(changeRegionLong || changeRegionLat) [self setRegion:currentRegion animated:YES];

}

-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{

    [scrollView setContentOffset:scrollView.contentOffset animated:YES];
}

@end
    
已邀请:
通过实施:
-(void) mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
您应该能够将区域重置为您的特定区域。 查看该答案,以获取有关如何执行此操作的示例。 带来更精确的事件但更复杂的另一种解决方案(我目前正在成功地将其用于其他需求)是子类
MKMapView
并重写了
UIScrollViewDelegate
协议。 如果这样做,请确保这样调用
super
实现:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  if ([super respondsToSelector:@selector(scrollViewWillBeginDragging:)])
    [super scrollViewWillBeginDragging:scrollView];
  // do what you want
}
注意:虽然该协议是公共协议,但是
MKMapView
的实现方式未知,并且可能与iOS版本不同,因此在使用with8ѭ进行检查之前更安全。您必须调用超级实现,否则Map将无法正常工作。     
在尝试了不同的受限MKMapView方法之后,我得出结论,使用mapDidChange并重置(如果您的中心点超出了边界)效果最好,并带有动画:是 这是我的操作方法(使用新西兰纬度/长跨度/中点)。
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{ 
  if ((mapView.region.span.latitudeDelta > 15.589921 ) || (mapView.region.span.longitudeDelta > 175.836914) ) {
    CLLocationCoordinate2D centerCoord = CLLocationCoordinate2DMake(-41.162114, 172.836914);

    MKCoordinateSpan spanOfNZ = MKCoordinateSpanMake(13.589921, 14.062500 );

    MKCoordinateRegion NZRegion = MKCoordinateRegionMake(centerCoord, spanOfNZ);

    [mapView setRegion: NZRegion animated: YES];
  }

 if (abs(abs(mapView.region.center.latitude) - 41.162114) > (13.589921 / 2) ) {
    CLLocationCoordinate2D centerCoord = CLLocationCoordinate2DMake(-41.162114, 172.836914);

    MKCoordinateSpan spanOfNZ = MKCoordinateSpanMake(13.589921, 14.062500 );

    MKCoordinateRegion NZRegion = MKCoordinateRegionMake(centerCoord, spanOfNZ);

    [mapView setRegion: NZRegion animated: YES];

  }

  if (abs(abs(mapView.region.center.longitude) - 172.836914) > (14.062500 / 2) ) {
    CLLocationCoordinate2D centerCoord = CLLocationCoordinate2DMake(-41.162114, 172.836914);

     MKCoordinateSpan  spanOfNZ = MKCoordinateSpanMake(13.589921, 14.062500 );

     MKCoordinateRegion NZRegion = MKCoordinateRegionMake(centerCoord, spanOfNZ);

     [mapView setRegion: NZRegion animated: YES];
  }
}
    
我知道这是一个很老的问题,但是要避免无限循环,您可以尝试执行以下操作:https://stackoverflow.com/a/4126011/1234011 基本上设置一个标志,以防止在设置该值的情况下再次触发回调。 肯     

要回复问题请先登录注册