5

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 remaining part of the map.

Iqbal Khan
  • 4,416
  • 6
  • 41
  • 78

2 Answers2

12

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<MKOverlay>)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.

Community
  • 1
  • 1
  • 12
    The code to create a world-encompassing `MKPolygon` needs adjustment for iOS 7. If you try to create a 4-point polygon that spans more than 180° longitude, it reverses itself to be mod 180: a 200° polygon becomes 20°, and a 360° like here, becomes 0. To work around this issue, we need to add 2 extra points in between: `CLLocationCoordinate2D worldCoords[6] = { {90, 0}, {90, 180}, {-90,180}, {-90,0}, {-90,-180}, {90,-180} };` – SaltyNuts Nov 05 '13 at 19:38
  • @SaltyNuts, Anna I am able to achieve the expected behavior by using world coordinates proposed by you guys, thanks for that :), however I am facing one issue - when I am trying to pan the map towards left or right I can observe that fill color is not covering beyond a range - http://i.stack.imgur.com/QSDQc.png, is there any way I can increase the fill color range? Please suggest. – Devarshi Aug 14 '15 at 07:29
0

You can specify the region as an MKCoordinateRegion and then tell an MKMapView instance to only show that region using the setRegion and regionThatFits message.

Alternatively you could use the visibleMapRect property instead of the region. This might better fit your needs.

In short read the MKMapView Class Reference document from Apple.

Lifting from some code I've done in the past that assumes a mapView and a given location called locationToShow I used an MKCoordinateRegion.

- (void) goToLocation {

    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta=0.01;
    span.longitudeDelta=0.01;

    region.span=span;
    region.center=locationToShow;
    [mapView setRegion:region animated:TRUE];
    [mapView regionThatFits:region];
}
ditkin
  • 6,046
  • 1
  • 28
  • 36