3

我正在使用 mapkit 为 iOS 制作一个应用程序。我想将地图的边界限制在特定的地区/国家。有没有办法做到这一点?

4

2 回答 2

1

这是对先前答案的进一步说明。请注意,世界不同地区的情况会有所不同:

    if(neCoord.latitude > myNorthEast.latitude){
        MKCoordinateRegion newRegion;
        newRegion.span = region.span;
        CLLocationCoordinate2D newCenter;
        newCenter.longitude = region.center.longitude;
        newCenter.latitude = myNorthEast.latitude - region.span.latitudeDelta / 2;
        newRegion.center = newCenter;
        [mapView setRegion:newRegion animated:NO];

    } else if(swCoord.latitude < mySouthWest.latitude){
        MKCoordinateRegion newRegion;
        newRegion.span = region.span;
        CLLocationCoordinate2D newCenter;
        newCenter.longitude = region.center.longitude;
        newCenter.latitude = mySouthWest.latitude + region.span.latitudeDelta / 2;
        newRegion.center = newCenter;
        [mapView setRegion:newRegion animated:NO];

    } else if(neCoord.longitude > myNorthEast.longitude){
        MKCoordinateRegion newRegion;
        newRegion.span = region.span;
        CLLocationCoordinate2D newCenter;
        newCenter.longitude = myNorthEast.longitude - region.span.longitudeDelta / 2;
        newCenter.latitude = region.center.latitude;
        newRegion.center = newCenter;
        [mapView setRegion:newRegion animated:NO];

    } else if(swCoord.longitude < mySouthWest.longitude){
        MKCoordinateRegion newRegion;
        newRegion.span = region.span;
        CLLocationCoordinate2D newCenter;
        newCenter.longitude = mySouthWest.longitude + region.span.longitudeDelta / 2;
        newCenter.latitude = region.center.latitude;
        newRegion.center = newCenter;
        [mapView setRegion:newRegion animated:NO];
    }
于 2014-03-07T16:50:53.207 回答
1

没有办法告诉地图不要滚动出某个区域。我能想到的唯一方法是在你碰到一个栅栏时阻止用户滚动。下面的示例是在没有测试或编译的情况下编写的,因此您可能需要自己调整它,但希望它能让您开始..

视图控制器.h

CLLocationCoordinate2D myNorthEast, mySouthWest;

视图控制器.m

-(void)viewDidLoad{
    myNorthEast = CLLocationCoordinate2DMake(lat1,lon1);
    mySouthWest = CLLocationCoordinate2DMake(lat2,lon2);
    [super viewDidLoad];
}

-(void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{
    /*
    / Check if the map region is going to change outside your fence.
    / If so, programmatically set it back to the edge of your fence.
    */

    if(!animated){
        return; // Don't want to get stuck in a loop after you set your region below.
    }

    MKCoordinateRegion region = [mapView region];

    // You will need to get the NE and SW points of the new region to compare
    // First we need to calculate the corners of the map so we get the points
    CGPoint nePoint = CGPointMake(mapView.bounds.origin.x + mapView.bounds.size.width, mapView.bounds.origin.y);
    CGPoint swPoint = CGPointMake(mapView.bounds.origin.x, bounds.origin.y + mapView.bounds.size.height);

    // Then transform those point into lat,lng values
    CLLocationCoordinate2D neCoord;
    neCoord = [mapView convertPoint:nePoint toCoordinateFromView:mapView];

    CLLocationCoordinate2D swCoord;
    swCoord = [mapView convertPoint:swPoint toCoordinateFromView:mapView];

    /*
        You will need to mess around with the lat/lon & sign of the new center calculation for the other cases.
    */
    if(neCoord.latitude > myNorthEast.latitude){
        MKCoordinateRegion newRegion;
        newRegion.span = region.span;
        CLLocationCoordinate2D newCenter;
        newCenter.longitude = region.center.longitude;
        newCenter.latitude = myNorthEast.latitude - region.span.latitudeDelta;
        newRegion.center = newCenter;
        [mapView setRegion:newRegion animated:NO];
    }else if(neCoord.longitude < myNorthEast.longitude){

    }else if(swCoord.latitude < mySouthWest.latitude){

    }else if(swCoord.longitude > mySouthWest.longitude){

    }
}

这部分来自这个答案: Getting the bounds of an MKMapvIew

于 2012-09-13T06:07:44.547 回答