使用MapKit显示特定区域 [英] Showing Specific Region Using MapKit
问题描述
我想知道是否可以使用Map Kit仅显示地图上的特定区域而不是整个世界地图。
如果我想在我的应用程序中显示亚洲地图,那么地图套件会隐藏地图的剩余部分。
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.
推荐答案
要处理地图套件隐藏地图的剩余部分要求,您可以做的一件事就是创建一个黑色多边形覆盖层,覆盖整个世界,在亚洲(或您喜欢的任何地方)使用切口。
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).
例如,初始化地图的地方(例如,在viewDidLoad中):
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];
并实现 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;
}
这看起来像这样:
如果您还希望限制用户滚动到亚洲以外或缩放太远,然后您还需要手动执行此操作。 限制MKMapView滚动中描述了一种可能的方法。用 asiaOverlay
替换该答案中的 theOverlay
。
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
.
这篇关于使用MapKit显示特定区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!