MKMapView的界限?

时间:2010-01-18 01:06:26

标签: objective-c cocoa-touch mkmapview

任何人都知道在MKMapView上实现边界的简单方法。例如,我想告诉地图它只能停留在一个区域。我希望用户能够滚动,但当他们到达某个纬度/经度时让它们停止。我尝试使用willChangeRegion:delegate但是这让我感到困惑。感谢。

3 个答案:

答案 0 :(得分:2)

我刚刚写了一篇关于此的博文,请在此处查看:http://blog.jamgraham.com/blog/2012/04/29/adding-boundaries-to-mkmapview/

我参与了一个需要锁定MKMapView边界的项目。由于苹果没有提供开箱即用的解决方案,因此我们将子类化为MKMapView。这非常简单,您可以访问位于地图中的UIScrollView以及MKMapView中的其他有趣对象。

现在想看看代码?它可以在这里找到:https://github.com/jamgraham/geoFencer

锁定MKMapView的边界会调用两个常规步骤。首先是将缩放级别锁定为最小值和最大值。其次是防止用户滚动到您创建的代工厂之外。让我们通过查看我们需要覆盖的代表

来完成这些操作

1。缩放级别

这很简单。目标是在用户缩放时检查缩放级别,并在用户停止缩放时再次检查缩放级别。

 -(void)scrollViewDidZoom:(UIScrollView *)scrollView;
 -(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale;

2。滚动边界

这有点复杂。当用户在UIScrollView中滚动地图时,我们希望确保它们不会超出边界,如果它们确实存在,我们想要阻止它们。我们可以通过在看到用户超出边界时将地图重新​​定位到边界来实现这一点。这种情况发生得足够快,用户感觉他们已经撞墙而不是地图四处乱窜。以下是我们需要关注的代表:

 -(void)scrollViewDidScroll:(UIScrollView *)scrollView;
 -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;

3。复制&粘贴

随意复制并粘贴下面的自定义MKMapView。在显示地图的UIViewController中,自定义地图视图需要设置边界

ViewController.m

    //Set map boundaries
    mapView.showsUserLocation = YES;
    [mapViewsetZoomMax:0.015551];
    [mapViewsetZoomMin: 0.346360];
    mapView.top = 37.773498;
    mapView.bottom = 37.745130;
    mapView.left = -122.430365;
    mapView.right = -122.401623;

GeoMapView.h

@interface GeoMapView : MKMapView
{
    MKCoordinateRegion cordRegion;
    CLLocationCoordinate2D oldcenter;
    double left;
    double right;
    double top;
    double bottom;
    double zoomMax;
    double zoomMin;
}

-(void)checkZoom;
-(void)checkScroll;

@property (nonatomic) double left;
@property (nonatomic) double right;
@property (nonatomic) double top;
@property (nonatomic) double bottom;
@property (nonatomic) double zoomMax;
@property (nonatomic) double zoomMin;

@end

GeoMapView.m

#import "GeoMapView.h"

@implementation GeoMapView
@synthesize  left, right, top, bottom,zoomMax,zoomMin;

- (id)initWithFrame:(CGRect)frame
{
    self = [superinitWithFrame:frame];
    if (self) {

    }
    returnself;
}

-(void)scrollViewDidZoom:(UIScrollView *)scrollView {
    [selfcheckZoom];    
}


-(void)checkZoom{
    UIScrollView * scroll = [[[[selfsubviews] objectAtIndex:0] subviews] objectAtIndex:0];

    if (scroll.zoomScale < zoomMax) {
        NSLog(@"Reached Max Zoom");
        [scroll setZoomScale:zoomMaxanimated:NO];
    }

    if (scroll.zoomScale >= zoomMin) {
        NSLog(@"Reached Min Zoom");
        [scroll setZoomScale:zoomMinanimated:NO];

    }
}

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
    UIScrollView * scroll = [[[[selfsubviews] objectAtIndex:0] subviews] objectAtIndex:0];

    if (scroll.zoomScale > zoomMin) {
        [scroll setZoomScale:zoomMinanimated:NO];
    }   
}

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

    [selfcheckScroll];

}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [selfcheckScroll];
}

-(void)checkScroll{
    @try{
        cordRegion = self.region;

        CLLocationCoordinate2D center = self.region.center;
        CLLocationCoordinate2D northWestCorner, southEastCorner;
        northWestCorner.latitude  = center.latitude  + (self.region.span.latitudeDelta  / 2.0);
        northWestCorner.longitude = center.longitude - (self.region.span.longitudeDelta / 2.0);
        southEastCorner.latitude  = center.latitude  - (self.region.span.latitudeDelta  / 2.0);
        southEastCorner.longitude = center.longitude + (self.region.span.longitudeDelta / 2.0);

        CLLocationCoordinate2D newcenter;
        newcenter.latitude = self.region.center.latitude;
        newcenter.longitude = self.region.center.longitude;

        //LEFT
        CLLocationDegrees farLeft = left;
        CLLocationDegrees snapToLeft = farLeft + (self.region.span.longitudeDelta  / 2.0);
        if (northWestCorner.longitude < farLeft)
        {
            newcenter.longitude = snapToLeft;
            cordRegion = self.region;
        }

        //RIGHT
        CLLocationDegrees r = (self.region.span.longitudeDelta / 2.0);
        CLLocationDegrees farRight = right;
        CLLocationDegrees snapToRight = farRight - r;
        if (southEastCorner.longitude > farRight)
        {
            newcenter.longitude = snapToRight;
        }

        //TOP
        CLLocationDegrees farTop = top;
        CLLocationDegrees snapToTop = top - (self.region.span.latitudeDelta  / 2.0);
        if (northWestCorner.latitude > farTop)
        {
            newcenter.latitude = snapToTop;
        }

        //BOTTOM
        CLLocationDegrees farBottom = bottom;
        CLLocationDegrees rr = (self.region.span.latitudeDelta  / 2.0);
        CLLocationDegrees snapToBottom = bottom + rr;
        if (southEastCorner.latitude < farBottom)
        {            
            newcenter.latitude = snapToBottom;
        }

        [selfsetCenterCoordinate:newcenter  animated:NO];

    }
    @catch (NSException *e) {
    }
    @finally {}
}


@end

目前在地理围栏中还有很多很酷的东西。一些想法:

  • 输入地址并将地图锁定到该地址
  • 使用地图叠加层进行地理围栏
  • 在用户实际访问这些实际位置时解锁地图区域的物理游戏。

答案 1 :(得分:0)

因此,我将继续回答我自己的问题,并说没有 EASY 方式来实现此行为。你不仅需要检查边界,还要检查滚动的方向,控制台一直告诉我:滚动后的最后一次触摸但是scrollViewDidEndDragging:willDecelerate:没有被调用!我想我还有更多的研究要做......

答案 2 :(得分:0)

我一直在做一些研究,发现没有简单的方法。

所以我的解决方案是将地图锁定在某个区域,并让包含MKMapView的scrollView放大和缩小。