2012-04-14 29 views
0

在我的應用程序中我想添加覆蓋圖來映射,當用戶拖動滑塊時,覆蓋圖 circle radius必須改變,但有時會拋出線程異常。 我的代碼:MKMapView addOverlays線程錯誤

- (void)addCircleWithRadius:(NSNumber *)radius 
    { 

     MKCircle *circle = [MKCircle circleWithCenterCoordinate:userlocation radius:[radius doubleValue] ]; 
     [circle setTitle:@"background"]; 


     MKCircle *circleLine = [MKCircle circleWithCenterCoordinate:userlocation radius:[radius doubleValue]]; 
     [circleLine setTitle:@"line"]; 

     NSArray *overlays = [NSArray arrayWithObjects:circle,circleLine, nil]; 
     [mapView addOverlays:overlays]; 

    } 

-(void)slider_change:(id)sender 
{ 
    @try { 
     [mapView removeOverlays:[mapView overlays]]; 
     if([mapView.overlays count] == 0) 
     { 
      CLLocationCoordinate2D _coordinate ; 
      _coordinate.latitude = 35.692995; 
      _coordinate.longitude = 51.421509; 
      NSNumber *value = [NSNumber numberWithFloat:slider.value * 1000]; 
      [self addCircleWithRadius:value]; 
     } 
    } 
    @catch (id exception) { 

    } 

} 

回答

0
-(IBAction)slider_action:(id)sender 
{ 

    [map removeOverlays:[map overlays]]; 

    double radius1 = (slider.value * 5000); 

    NSLog(@"%f",radius1); 

    [self addCircleWithRadius:radius1 addCircleWithCoordinate:theCoordinate1]; 
} 

-(void) addCircleWithRadius:(double)radius1 addCircleWithCoordinate: (CLLocationCoordinate2D) coordinate 
{ 
    MKCircle *circle1 = [MKCircle circleWithCenterCoordinate:coordinate radius:radius1]; 

    [map addOverlay:circle1]; 
} 
- (MKOverlayView *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay 
{ 

    MKCircle* circle1 = overlay; 
    MKCircleView *circleView = [[MKCircleView alloc] initWithCircle:circle1]; 

    circleView.alpha = 0.25; 
    circleView.lineWidth = 2.0; 

    circleView.strokeColor = [UIColor blackColor]; 
    circleView.fillColor = [UIColor redColor]; 

    return circleView; 
}