2013-03-18 39 views
0

我有地圖視圖,我喜歡使用覆蓋方法繪製線,我添加了兩個位置的緯度和經度,當我按下按鈕時畫線但當我第二次按下新的我提供的座標paire和新的線圖覆蓋,但以前的覆蓋路徑已消失,但我需要在同一地圖上的previou路徑也堅持覆蓋線在iOS6中的地圖視圖

這裏是我的代碼。

- (IBAction)refreshMapMethod:(id)sender 

{ 


int kk=[[NSUserDefaults standardUserDefaults]integerForKey:@"ham"]; 




    if (kk==1) 
    { 
     CLLocationCoordinate2D coordinateArray[2]; 
     coordinateArray[0] = CLLocationCoordinate2DMake(12.915181,+77.626055); 
     coordinateArray[1] = CLLocationCoordinate2DMake(12.892156, +77.582188); 
     self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2]; 
     [self.myMapView setVisibleMapRect:[self.routeLine boundingMapRect]]; 

     [self.myMapView addOverlay:self.routeLine]; 
     [[NSUserDefaults standardUserDefaults]setInteger:2 forKey:@"ham" ]; 


    } 


    if (kk==2) 
    { 
     CLLocationCoordinate2D coordinateArray[2]; 
     coordinateArray[0] = CLLocationCoordinate2DMake(12.892156,+77.426055); 
     coordinateArray[1] = CLLocationCoordinate2DMake(12.892156, +77.582188); 
     self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2]; 
     [self.myMapView setVisibleMapRect:[self.routeLine boundingMapRect]]; 

     [self.myMapView addOverlay:self.routeLine]; 
     [[NSUserDefaults standardUserDefaults]setInteger:3 forKey:@"ham" ]; 


    } 
    if (kk==3) 
    { 
     CLLocationCoordinate2D coordinateArray[2]; 
     coordinateArray[0] = CLLocationCoordinate2DMake(12.892156, +77.382188); 
     coordinateArray[1] = CLLocationCoordinate2DMake(12.892156, +77.282188); 
     self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:2]; 
     [self.myMapView setVisibleMapRect:[self.routeLine boundingMapRect]]; 

     [self.myMapView addOverlay:self.routeLine]; 
     [[NSUserDefaults standardUserDefaults]setInteger:3 forKey:@"ham" ]; 


    } 



    CLLocationCoordinate2D zoomLocation; 
    zoomLocation.latitude= 12.915181; 
    zoomLocation.longitude=77.626055; 

    MKCoordinateRegion viewRegion=MKCoordinateRegionMakeWithDistance(zoomLocation, 3*METERS_PER_MILE, 3*METERS_PER_MILE); 

    [self.myMapView setRegion:viewRegion animated:YES]; 


[[NSUserDefaults standardUserDefaults]setInteger:2 forKey:@"change" ]; 


} 

調用覆蓋方法..

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay 


{ 

    if(overlay == self.routeLine) 
    { 
     if(nil == self.routeLineView) 
     { 
      self.routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine]; 



      self.routeLineView.fillColor = [UIColor redColor]; 
      self.routeLineView.strokeColor = [UIColor redColor]; 
      self.routeLineView.lineWidth = 5; 

     } 

     return self.routeLineView; 
    } 

    return nil; 
} 

請給我提供在TYE相同方式或其他任何替代方式的解決方案。

比你。

回答

1

這是因爲您只允許viewForOverlay函數返回self.routeline的視圖,並且只有其中之一。每撥打viewForOverlay將會返回零,因此不會被繪製。你需要做的是繪製所有的覆蓋。

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay 
{ 
    MKPolylineView* routeLineView = [[MKPolylineView alloc] initWithPolyline:(MKPolyLine)overlay]; 
    routeLineView.fillColor = [UIColor redColor]; 
    routeLineView.strokeColor = [UIColor redColor]; 
    routeLineView.lineWidth = 5; 

    return routeLineView; 
} 

你可能需要做一些更多的東西,如實際檢查的覆蓋是一個折線第一,但是這應該足以讓你去。