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

标签 iphone ios mapkit android-mapview region

我有以下问题:

我有一个“绘制的 map ”(图像),我将其作为叠加层添加到 MapView。没问题..但我需要将 MapView 限制在覆盖区域,因此用户无法在该区域之外滚动/缩放..但应该可以在“边界”内滚动/缩放"的叠加 - 意味着我不能只禁用 MapView 的缩放/滚动。

关于这个主题有什么想法/解决方案吗?使用 MapView/-Kit 的原因是我需要在自定义 map 中添加各种 POI。当仅使用 ImageView+ScrollView 来呈现自定义 map 时,这可能会变得更加复杂。

我对这个主题进行了很多研究,但没有找到一个好的解决方案。

感谢任何帮助!

最好的问候, 基督徒

编辑:这是我们的解决方案: 您提供左上角和右下角坐标来限制 map 。 (最小)缩放级别也受到限制。我已经停用了减速功能,您可以从 map 上弹开一点(以获得更好的性能/用户体验)。我在叠加层中添加了约 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

最佳答案

在尝试了有限 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];
  }
}

关于iphone - iOS - 如何将 MapView 限制在特定区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5680896/

相关文章:

iphone - 将 WebGL 游戏移植到 iPhone 的原生 OpenGL?

ios - 在 map View 上显示 CLRegion

ios - 在 MKlocalSearch mapkit 中扩展搜索区域

ios - 如何实现自定义动画天气 MapKit 叠加 - iOS、Swift

iphone - 导航栏出现在带有新 iOS7 SDK 的 View 上

objective-c - iPhone 6 Plus 上的 UISplitViewController 旋转大师 Master

iphone - 为什么我的 UIView 子类在调用 setNeedsDisplay 后没有重绘?

ios - 在iOS上的应用程序内销售商品

ios - Facebook SDK 4.0 AppInvite 没有收到任何通知

ios - Swift:AVAudioPlayer 的 addPeriodicTimeObserverForInterval