0
votes

I want to show different annotations at each pin,with my image named "01.png" to "08.png". I set integer a random number from 1~8

integer = arc4random() % 8 + 1;

then set the method -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation

if(annotation == mapView.userLocation){
    return nil;
}
NSString *identifier = @"Store";

MKAnnotationView *result = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

if(result == nil){
    result = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
    result.annotation = annotation;
}

NSString * imageName = [NSString stringWithFormat:@"0%i",integer];

UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]];
result.image = image;

return result;

but the mapView show the same annotations,it will show different annotations at each time I open the apps.

How to show different annotations at each pin?

Thank you very much!

1

1 Answers

1
votes

You must put the randomization inside the annotation method. A variable has one value until it is changed, it does not get a new random value every time you read from it.