[英]Showing Specific Region Using MapKit
我想知道是否可以使用Map Kit在地圖上僅顯示特定區域而不是整個世界地圖。 就像我想在我的應用程序中顯示亞洲地圖一樣,地圖套件隱藏了地圖的剩余部分。
要處理“地圖套件隱藏地圖的剩余部分”要求,您可以做的一件事就是創建一個黑色多邊形覆蓋圖,覆蓋整個世界,在亞洲(或您喜歡的任何地方)切口。
例如,初始化地圖的位置(例如,在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滾動中描述了一種可能的方法。 用theOverlay
替換該答案中的asiaOverlay
。
您可以將區域指定為MKCoordinateRegion,然后告訴MKMapView實例僅使用setRegion和regionThatFits消息顯示該區域。
或者,您可以使用visibleMapRect屬性而不是區域。 這可能更適合您的需求。
簡而言之,請閱讀Apple的MKMapView Class Reference文檔。
從過去的一些代碼中提取,假設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.