简体   繁体   中英

How to have multiple annotations on a mapview based on an array

The below code is what I have used so far, and it loops through every object in the array correctly, but when I try to make them all display on one map it only adds the last obeject in the array to the map, not all 20 or so I want to display.

   self.clientTable = [ClientDatabase database].clientTable;

    ClientTable *info = nil;
    [_nameLabel setText:info.name];
    [_stateLabel setText:info.state];

    //change the string to doubles for the map GPS co-ordinates
    double latDouble = [info.latMap doubleValue];
    double longDouble = [info.longMap doubleValue];

    NSLog(@"%d",[self.clientTable count]);

    int countArray = [self.clientTable count];

    for (int i=0;i<countArray;i++) {

        info = [self.clientTable objectAtIndex:i];
        info.uniqueId=i;
        NSLog(@" i = %d ; id = %d %@",i, info.uniqueId, info.name);

        //set up the map
        [super viewDidLoad];
        [mapView setMapType:MKMapTypeStandard];
        [mapView setZoomEnabled:YES];
        [mapView setScrollEnabled:YES];
        MKCoordinateRegion region = {{0.0,0.0},{0.0,0.0}}; 

        region.center.latitude = latDouble;
        region.center.longitude = longDouble;
        region.span.longitudeDelta =0.02;  //degrees of acuracy, most precise best for this time
        region.span.latitudeDelta =0.02;   //degrees of accuracy

        [mapView setRegion:region animated:YES]; 

        // set up the annotation point

        AllMap *annotationPoint = [[AllMap alloc] init]; 
        annotationPoint.title = info.name;
        annotationPoint.subtitle = info.state; 
        annotationPoint.coordinate = region.center; 
        [mapView addAnnotation:annotationPoint];
        annotationPoint.isAccessibilityElement=YES;
        //show annotation by default
        [mapView selectAnnotation:annotationPoint animated:YES];  

        [mapView setDelegate:self];

    }

Sorry if the code is rubbishy, i'm new to iPhone programming.

Thanks in advance :D

It looks like you're calling [super viewDidLoad] inside your for loop, which is probably resetting the mapView's annotations array. This method should only be called once, so if you move it before the for statement you may get better results.

why are you setting up the map inside of the loop where you are creating the annotations?

here is an old blog posting, but it covers the basics and should get you back on track

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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