我试图根据它们在数组中的位置来显示注释。到目前为止,我已将所有MKPointAnnotation添加到数组中并将其添加到[mapView addAnnotation:(NSArray*)]
。我还实现了-(void) mapView viewForAnnotation (-Id) MKAnnotation:annotation
委托方法。我试图传递注释数组并为不同的索引显示不同的注释,但它为每个位置显示相同的图像。我怎么能搞清楚。
这是我的示例代码:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSArray *frames = [NSArray arrayWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
[UIImage imageNamed:@"4.png"],
[UIImage imageNamed:@"5.png"],
nil];
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 20,20);
//btn.backgroundColor = [UIColor redColor];
[btn setTitle:@"▶️" forState:UIControlStateNormal];
UIView *anView=[[UIView alloc] init];
anView.backgroundColor=[UIColor redColor];
// NSLog(@"%i",array.count);
if([annotation isEqual:[mapView userLocation]])
{
//bail
return nil;
}
if([annotation isKindOfClass:[MKPointAnnotation class]]) {
MKAnnotationView *pin = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];;
int i ;
for (i = 0; i<array.count; i++) {
if(!pin){
pin = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
if (i==5){
UIImage *img = [UIImage imageNamed:@"person.png"];
pin.image = img;
}
else if (i==0){
UIImage *img = [UIImage imageNamed:@"1.png"];
NSLog(@"%i",i);
pin.image = img;
pin.rightCalloutAccessoryView = btn;//[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pin.leftCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeInfoLight];
pin.canShowCallout = YES;
}
else if (i==2){
UIImage *img = [UIImage imageNamed:@"3.png"];
NSLog(@"%i",i);
pin.image = img;
pin.rightCalloutAccessoryView = btn;//[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pin.leftCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeInfoLight];
pin.canShowCallout = YES;
}
//NSLog(@"%i",i);
}
}
return pin;
}
return nil;
}