2012-01-30 82 views
1

針在我的viewDidLoad:方法我已經設置這樣的MapView,這樣,如果用戶選擇了所有的位置,那麼它應該顯示所有的位置/引腳的觀點,如果只有一個那麼只有一個引腳觀點應該是所示。MapView的註解/在iPhone

- (void)viewDidLoad{ 

[mapView setMapType:MKMapTypeStandard]; 
[mapView setZoomEnabled:YES]; 
[mapView setScrollEnabled:YES]; 

if ([vehicleLabel.text isEqualToString:@"All Vehicles"]) { 

    MKCoordinateRegion region = {{0.0, 0.0},{0.0, 0.0}}; 
    region.center.latitude = 41.01860; 
    region.center.longitude = 28.96470; 
    region.span.longitudeDelta = 0.01f; 
    region.span.latitudeDelta = 0.01f; 
    [mapView setRegion:region animated:YES]; 

    [mapView setDelegate:self]; 

    Annotations *ann = [[Annotations alloc] init]; 
    ann.title = @"Vehicle A"; 
    ann.subtitle = @"VG 1"; 
    ann.coordinate = region.center; 

    [mapView addAnnotation:ann]; 

    NSMutableArray* annotations=[[NSMutableArray alloc] init]; 

    CLLocationCoordinate2D theCoordinate1; 
    theCoordinate1.latitude = 41.01760; 
    theCoordinate1.longitude = 30.96470; 

    CLLocationCoordinate2D theCoordinate2; 
    theCoordinate2.latitude = 41.01860; 
    theCoordinate2.longitude = 31.96470; 

    CLLocationCoordinate2D theCoordinate3; 
    theCoordinate3.latitude = 41.01360; 
    theCoordinate3.longitude = 25.96470; 

    CLLocationCoordinate2D theCoordinate4; 
    theCoordinate4.latitude = 41.01560; 
    theCoordinate4.longitude = 27.96470; 

    Annotations* myAnnotation1=[[Annotations alloc] init]; 

    myAnnotation1.coordinate=theCoordinate1; 
    [email protected]"Vehicle B"; 
    [email protected]"Group 1"; 

    Annotations* myAnnotation2=[[Annotations alloc] init]; 

    myAnnotation2.coordinate=theCoordinate2; 
    [email protected]"Vehicle C"; 
    [email protected]"Group 1"; 

    Annotations* myAnnotation3=[[Annotations alloc] init]; 

    myAnnotation3.coordinate=theCoordinate3; 
    [email protected]"Vehicle D"; 
    [email protected]"Group 1"; 

    Annotations* myAnnotation4=[[Annotations alloc] init]; 

    myAnnotation4.coordinate=theCoordinate4; 
    [email protected]"Vehicle E"; 
    [email protected]" Group 1"; 

    [mapView addAnnotation:myAnnotation1]; 
    [mapView addAnnotation:myAnnotation2]; 
    [mapView addAnnotation:myAnnotation3]; 
    [mapView addAnnotation:myAnnotation4]; 

    [annotations addObject:myAnnotation1]; 
    [annotations addObject:myAnnotation2]; 
    [annotations addObject:myAnnotation3]; 
    [annotations addObject:myAnnotation4]; 

    NSLog(@"Annotations: %d",[annotations count]); 
} 
else { 

MKCoordinateRegion region = {{0.0, 0.0},{0.0, 0.0}}; 
region.center.latitude = 41.01860; 
region.center.longitude = 28.96470; 
region.span.longitudeDelta = 0.01f; 
region.span.latitudeDelta = 0.01f; 
[mapView setRegion:region animated:YES]; 

[mapView setDelegate:self]; 

Annotations *ann = [[Annotations alloc] init]; 
ann.title = @"Vehicle A"; 
ann.subtitle = @"VG 1"; 
ann.coordinate = region.center; 

[mapView addAnnotation:ann]; 
}} 

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation{ 

MKPinAnnotationView *pinView = nil; 
if(annotation != mapView.userLocation) 
{ 
    static NSString *defaultPinID = @"aPin"; 
    pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; 
    if (pinView == nil) 
     pinView = [[[MKPinAnnotationView alloc] 
        initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease]; 
} else { 
} 
pinView.pinColor = MKPinAnnotationColorRed; 
pinView.canShowCallout = YES; 
pinView.animatesDrop = YES; 
return pinView;} 

在我PickerView didSelectRow method:我已經實現:

-(void)pickerView:(UIPickerView *)pickerViewG didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ 

if (component == kGroupComponent) { 
    NSString *selectedState = [self.vehicleGroup objectAtIndex:row]; 
    NSArray *array = [groupVehicles objectForKey:selectedState]; 
    self.vehiclesList = array; 
    [vehiclePicker selectRow:0 inComponent:kListComponent animated:YES]; 
    [vehiclePicker reloadComponent:kListComponent]; 
} 

NSInteger groupRow = [vehiclePicker selectedRowInComponent:kGroupComponent]; 
NSInteger vehicleRow = [vehiclePicker selectedRowInComponent:kListComponent]; 

NSString *group = [self.groupOfVehicles objectAtIndex:groupRow]; 
NSString *vehicle = [self.listVehicles objectAtIndex:vehicleRow]; 

groupLabel.text = [[NSString alloc] initWithFormat: @"%@", group]; 

vehicleLabel.text = [[NSString alloc] initWithFormat: @"%@", vehicle]; 

valueForOtherView = vehicleLabel.text;} 

這裏是我想知道的是,如果用戶選擇什麼從選擇器視圖中的單個車輛,那麼所有其他引腳的意見/地點應被隱藏/從視圖中移除,並且應該顯示唯一選定的位置/視圖視圖。

我怎麼做這個選擇器視圖didSelectRow method:

回答

0

我在與你相似的情況下做什麼,就是:

1)首先刪除所有註釋

for (id <MKAnnotation> anAnnotation in [NSArray arrayWithArray:mapView_.annotations]) { 
    [mapView_ removeAnnotation:anAnnotation]; 
} 

2),然後繪製選擇的:

[mapView_ addAnnotation:myAnnotation];