简体   繁体   English

使用MapKit显示特定区域

[英]Showing Specific Region Using MapKit

I want to know is it possible to show only specific region on map not the full world map using Map Kit. 我想知道是否可以使用Map Kit在地图上仅显示特定区域而不是整个世界地图。 Like if i want to show Asia map in my application then map kit hides remaining part of the map. 就像我想在我的应用程序中显示亚洲地图一样,地图套件隐藏了地图的剩余部分。

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): 例如,初始化地图的位置(例如,在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: 并实现viewForOverlay委托方法:

-(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: 这看起来像这样:

在此输入图像描述

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 . 限制MKMapView滚动中描述了一种可能的方法。 Replace theOverlay in that answer with asiaOverlay . theOverlay替换该答案中的asiaOverlay

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. 您可以将区域指定为MKCoordinateRegion,然后告诉MKMapView实例仅使用setRegion和regionThatFits消息显示该区域。

Alternatively you could use the visibleMapRect property instead of the region. 或者,您可以使用visibleMapRect属性而不是区域。 This might better fit your needs. 这可能更适合您的需求。

In short read the MKMapView Class Reference document from Apple. 简而言之,请阅读Apple的MKMapView Class Reference文档。

Lifting from some code I've done in the past that assumes a mapView and a given location called locationToShow I used an MKCoordinateRegion. 从过去的一些代码中提取,假设mapView和一个名为locationToShow的给定位置,我使用了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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM