如何随机显示注释
How to show annotations randomly
我想在每个图钉上显示不同的注释,我的图像名为“01.png”到“08.png”。
我将整数设置为 1~8
中的随机数
integer = arc4random() % 8 + 1;
然后设置方法-(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;
但是mapView显示相同的注解,每次打开app都会显示不同的注解
如何在每个引脚显示不同的注释?
非常感谢!
您必须将随机化放在注释方法中。一个变量只有一个值,直到它被改变,它不会在你每次从它读取时得到一个新的随机值。
我想在每个图钉上显示不同的注释,我的图像名为“01.png”到“08.png”。 我将整数设置为 1~8
中的随机数integer = arc4random() % 8 + 1;
然后设置方法-(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;
但是mapView显示相同的注解,每次打开app都会显示不同的注解
如何在每个引脚显示不同的注释?
非常感谢!
您必须将随机化放在注释方法中。一个变量只有一个值,直到它被改变,它不会在你每次从它读取时得到一个新的随机值。