Showing Specific Region Using MapKit

后端 未结 2 602
谎友^
谎友^ 2020-12-05 22:07

I want to know is it possible to show only specific region on map not the full world map using Map Kit. Like if i want to show Asia map in my application then map kit hides

2条回答
  •  旧巷少年郎
    2020-12-05 22:27

    To handle the "map kit hides remaining part of the map" requirement, one thing you can do is create a black polygon overlay that covers the whole world with a cutout over Asia (or wherever you like).

    For example, where you initialize the map (eg. in viewDidLoad):

    CLLocationCoordinate2D asiaCoords[4] 
        = { {55,60}, {55,150}, {0,150}, {0,60} };
          //change or add coordinates (and update count below) as needed 
    self.asiaOverlay = [MKPolygon polygonWithCoordinates:asiaCoords count:4];
    
    CLLocationCoordinate2D worldCoords[4] 
        = { {90,-180}, {90,180}, {-90,180}, {-90,-180} };
    MKPolygon *worldOverlay 
        = [MKPolygon polygonWithCoordinates:worldCoords 
                     count:4 
                     interiorPolygons:[NSArray arrayWithObject:asiaOverlay]];
                       //the array can have more than one "cutout" if needed
    
    [myMapView addOverlay:worldOverlay];
    

    and implement the viewForOverlay delegate method:

    -(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay
    {
        if ([overlay isKindOfClass:[MKPolygon class]])
        {
            MKPolygonView *pv = [[[MKPolygonView alloc] initWithPolygon:overlay] autorelease];
            pv.fillColor = [UIColor blackColor];
            pv.alpha = 1.0;
            return pv;
        }
    
        return nil;
    }
    

    This looks like this:

    enter image description here

    If you also want to restrict the user from scrolling beyond Asia or zooming too far out, then you'll need to do that manually as well. One possible way is described in Restrict MKMapView scrolling. Replace theOverlay in that answer with asiaOverlay.

提交回复
热议问题