how can I limit the map area to only one country in iOS?

半腔热情 提交于 2020-01-14 14:39:07

问题


I am making an app for iOS using mapkit. I want to limit the boundaries of the map only to a specific region/country. Is there a way to do this?


回答1:


There's no way to tell the map not to scroll out of a certain area. The only way I could think to do it would be to stop the user from scrolling when you hit one of your fences. The example below is written without testing or compiling at all so you may need to tweek it yourself but hopefully it'll get you started..

ViewController.h

CLLocationCoordinate2D myNorthEast, mySouthWest;

ViewController.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){

    }
}

Part of this comes from this answer: Getting the bounds of an MKMapvIew




回答2:


Here is a further specification of the previous answer. Note that this will be different for different parts of the world:

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


来源:https://stackoverflow.com/questions/12400062/how-can-i-limit-the-map-area-to-only-one-country-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!