0

In my app, I get the latitude and longitude along with some other data from a Parse query. However, only about 100 annotations get added, when there should be 156. I verify in console that it is getting the coordinates for the missing pieces on the map, it just simply doesn't add an annotation for them. Am I missing something obvious?

-(void) viewWillAppear:(BOOL)animated {
 CLLocationCoordinate2D coord = {.latitude =  15.8700320, .longitude =  100.9925410};
 MKCoordinateSpan span = {.latitudeDelta =  3, .longitudeDelta =  3};
 MKCoordinateRegion region = {coord, span};
 [mapViewUI setRegion:region];
 PFQuery *query = [PFQuery queryWithClassName:@"Share"];
    [query setLimit:1000];
 [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
 if (!error) {
 self.mapViewData = objects;
     NSLog(@"HOW MANY ARE THERE %lu", (unsigned long)[objects count]);
 for (int i=0;i<[objects count];i++)
 {
 // Question * q = [[Question alloc]init];

 PFObject * obj = [self.mapViewData objectAtIndex:i];
 NSLog(@"%@", obj);
 self.theObject = obj;

 NSString *string = obj[@"WhereAt"];


 NSArray *stringArray = [string componentsSeparatedByString: @","];

 CLLocationDegrees myLatitude = [[stringArray objectAtIndex:0] doubleValue];
 CLLocationDegrees myLongitude = [[stringArray objectAtIndex:1] doubleValue];
 CLLocationCoordinate2D coord2 = {.latitude =  myLatitude, .longitude =  myLongitude};
     NSLog(@"LATITUDE %@", [stringArray objectAtIndex:0]);
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 dateFormatter.dateFormat = @"MMM dd, yyyy";

 MKPointAnnotation *annotation2 = [[MKPointAnnotation alloc] init];
 [annotation2 setCoordinate:coord2];
 [annotation2 setTitle:obj[@"FamilyName"]];
 [annotation2 setSubtitle:obj[@"Result"]];

 [mapViewUI addAnnotation:annotation2];


 }


 } else {
 // Log details of the failure
 NSLog(@"Error: %@ %@", error, [error userInfo]);
 }


 }];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    if (annotation == mapViewUI.userLocation)
    {
        return nil;
    }
    else
    {

    MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] init];

    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"MMM dd, yyyy";

    if ([annotation.subtitle isEqualToString:@"Accepted Bible"]) {
        NSLog(@"Accepted");
        annotationView.pinTintColor = [UIColor greenColor];

    }
   else if ([annotation.subtitle isEqualToString:@"Requested Different Material"]) {
       NSLog(@"Different");

        annotationView.pinTintColor = [UIColor blackColor];

    }
   else if ([annotation.subtitle isEqualToString:@"Not Home"]) {
      NSLog(@"Not Home");

        annotationView.pinTintColor = [UIColor yellowColor];

    }
   else if ([annotation.subtitle isEqualToString:@"Rejected Bible"]) {
       NSLog(@"Rejected");

        annotationView.pinTintColor = [UIColor redColor];

    }



    return annotationView;
    }

    return nil;
}

1 Answers1

0

I don't know if this is the cause of your issue but you should be 'recycling' the annotation views like this:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(ClubPinAnnotation *)annotation {

    MKAnnotationView *pinView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"MyPinIdentifier"];

    if (!pinView) {
        pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MyPinIdentifier"];
    } else {    
        pinView.annotation = annotation;
    }

    // Set up the pin view properties

    return pinView;
}

Also, check that your lat/longs are correct. The map view will only call for the pin views it actually needs for the current view, so if you have some that are for instance at (0,0) they won't be shown until you zoom out to include the coast of Africa.

norders
  • 1,160
  • 9
  • 13