iPhone中的MapView注释/引脚

时间:2012-01-30 09:52:23

标签: iphone objective-c ios cocoa-touch mapkit

在我的viewDidLoad:方法中,我已经设置了MapView,这样如果用户选择了所有位置,那么它应该显示所有位置/引脚视图,如果只有一个,则应该只显示一个引脚视图。

- (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;
    myAnnotation1.title=@"Vehicle B";
    myAnnotation1.subtitle=@"Group 1";

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

    myAnnotation2.coordinate=theCoordinate2;
    myAnnotation2.title=@"Vehicle C";
    myAnnotation2.subtitle=@"Group 1";

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

    myAnnotation3.coordinate=theCoordinate3;
    myAnnotation3.title=@"Vehicle D";
    myAnnotation3.subtitle=@"Group 1";

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

    myAnnotation4.coordinate=theCoordinate4;
    myAnnotation4.title=@"Vehicle E";
    myAnnotation4.subtitle=@" 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;}

我想知道的是,如果用户从选择器视图中选择单个车辆,那么应该从视图中隐藏/移除所有其他引脚视图/位置,并且应显示唯一选定的位置/引脚视图。 / p>

如何在此选择器视图didSelectRow method:中执行此操作?

1 个答案:

答案 0 :(得分:0)

我在类似你的案件中所做的是:

1)首先删除所有注释

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

2)然后绘制所选的那些:

[mapView_ addAnnotation:myAnnotation];