2011-12-30 208 views
4

我想知道是否可以僅使用Map Kit在地圖上顯示特定區域而不是全部世界地圖。 如果我想在我的應用程序中顯示亞洲地圖,那麼地圖工具包隱藏地圖的其餘部分。使用MapKit顯示特定區域

回答

9

處理「地圖包隱藏地圖剩餘部分」的要求,有一兩件事你可以做的是創造一個黑色多邊形疊加與切口亞洲各地(或你喜歡的地方)覆蓋了整個世界。

例如,當你初始化地圖(在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; 
} 

這看起來是這樣的:

enter image description here

如果你還想要r限制用戶滾動到亞洲以外或縮小太遠,那麼您也需要手動執行此操作。在Restrict MKMapView scrolling中描述了一種可能的方式。用asiaOverlay代替theOverlay

+8

創建包含世界的'MKPolygon'的代碼需要iOS 7的調整。如果您嘗試創建一個跨越180°以上經度的4點多邊形,它將自身反轉爲180模式:一個200°多邊形爲了解決這個問題,我們需要在兩者之間增加2個額外的點:CLLocationCoordinate2D worldCoords [6] = {{90,0},{90,180},等等。 {-90,180},{-90,0},{-90,-180},{90,-180}};' – SaltyNuts 2013-11-05 19:38:48

+0

@SaltyNuts,Anna我可以通過使用你們提出的世界座標來達到預期的行爲,謝謝你:),但是我面臨着一個問題 - 當我試圖向左或向右平移地圖時,我可以觀察到填充顏色沒有覆蓋範圍 - http://i.stack.imgur.com /QSDQc.png,有什麼辦法可以增加填充顏色範圍?請建議。 – Devarshi 2015-08-14 07:29:19

0

您可以將區域指定爲MKCoordinateRegion,然後告訴MKMapView實例僅使用setRegion和regionThatFits消息顯示該區域。

或者,您可以使用visibleMapRect屬性而不是區域。這可能會更好地滿足您的需求。

簡而言之,請閱讀Apple提供的MKMapView類參考文檔。

從我過去做過的假設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]; 
}