简体   繁体   English

iPhone中的MapView注释/图钉

[英]MapView annotations/pins in iPhone

In my viewDidLoad: method I have set the MapView like this so that if user has selected all the locations then it should show all the locations/pin views and if only one then only one pin view should be shown. 在我的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;}

And in my PickerView didSelectRow method: I have implemented: 在我的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;}

Here what I want to know is that what if user selects a single vehicle from the picker view then all other pin views/ locations should be hidden/removed from the view and the only selected location/ pin view should be shown. 在这里,我想知道的是,如果用户从选择器视图中选择单个车辆,那么应该从视图中隐藏/删除所有其他销视图/位置,并且应该显示唯一选择的位置/销视图。

How can I do that in this picker view didSelectRow method: ? 如何在此选择器视图didSelectRow method:做到这一点?

What I'm doing in a case similar to yours, is: 在与您类似的情况下,我正在做的是:

1) remove first all the annotations 1)首先删除所有注释

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

2) and then paint the ones selected: 2),然后绘制所选的内容:

[mapView_ addAnnotation:myAnnotation];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM