2012-06-02 44 views
3

我無法在MKOverlayView中添加兩種不同顏色的MKPolylineView。任何想法如何實現這一目標? 感謝添加兩個MKPolylineView

這裏是我的代碼:

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay 
{ 
MKOverlayView* overlayView = nil; 
UIColor *mycolor; 

self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease]; 

mycolor = [UIColor colorWithRed:85.0/255.0 green:133.0/255.0 blue:255.0/255.0 alpha:0.6]; 
self.routeLineView.fillColor = mycolor; 
self.routeLineView.strokeColor = mycolor; 
self.routeLineView.lineWidth = 15; 
[overlayView addSubview:self.routeLineView]; 

self.routeLineView2 = [[[MKPolylineView alloc] initWithPolyline:self.routeLine2] autorelease]; 
mycolor = [UIColor colorWithRed:85.0/255.0 green:19.0/255.0 blue:25.0/255.0 alpha:0.6]; 
self.routeLineView2.fillColor = mycolor; 
self.routeLineView2.strokeColor = mycolor; 
self.routeLineView2.lineWidth = 15; 
[overlayView addSubview:self.routeLineView2]; 

return overlayView; 
} 

回答

0

viewForOverlay方法將分別爲您添加到地圖上每個覆蓋被調用。因此,在該方法中,您只返回當前正在調用的疊加層的疊加視圖(即參數overlay)。

檢查它正在請求視圖的哪個覆蓋圖,並僅爲該覆蓋圖創建並返回一個視圖。

例如:

if (overlay == self.routeLine) 
{ 
    //create and return overlay view for routeLine... 
    //set color, etc... 
    return self.routeLineView; 
} 
else 
if (overlay == self.routeLine2) 
{ 
    //create and return overlay view for routeLine2... 
    //set color, etc... 
    return self.routeLineView2; 
} 

return nil; 

不要做任何addSubview東西。只需創建疊加視圖並將其返回即可。