我在地图视图上显示了一些地图图钉。我无法弄清楚为什么在触摸注释时标注不会显示。这是我的代码。我在 viewDidLoad 方法中有一些条件代码,可以根据类别 ID 显示不同的图钉图像。自定义引脚显示正确,但是当您触摸它们时,没有任何反应。
感谢您对此的任何帮助。
- (void)viewDidLoad {
[super viewDidLoad];
[self requestData];
CLLocationCoordinate2D centerCoord = CLLocationCoordinate2DMake(44.2632, 11.4403);
MKCoordinateSpan span;
span.latitudeDelta=5;
span.longitudeDelta=5;
MKCoordinateRegion ITRegion = MKCoordinateRegionMake(centerCoord, span);
ITRegion.span=span;
[mapView setRegion: ITRegion animated: YES];
mapView.delegate=self;
[self.mapView setShowsUserLocation:YES];
for (int i=0; i<[publicDataArray count]; i++) {
MKPointAnnotation* annotation= [MKPointAnnotation new];
CLLocationCoordinate2D coordinate;
coordinate.latitude = [[[[publicDataArray objectAtIndex:i] objectForKey:@"address"] objectForKey:@"lat"] doubleValue];
coordinate.longitude = [[[[publicDataArray objectAtIndex:i] objectForKey:@"address"] objectForKey:@"lng"] doubleValue];
NSString *catId = [NSString stringWithFormat:@"%@",[[publicDataArray objectAtIndex:i] objectForKey:@"category_id"]];
if ([catId isEqual: @"213"]) {
annView.image = [UIImage imageNamed:@"comprareMap.png"];//sets image for pin
} else if ([catId isEqual:@"217"]) {
annView.image = [UIImage imageNamed:@"vedereMap.png"];//sets image for pin
} else if ([catId isEqual:@"221"]) {
annView.image = [UIImage imageNamed:@"mangiareMap.png"];//sets image for pin
}
annotation.coordinate = coordinate;
[mapView addAnnotation: annotation];
}
}
- (MKAnnotationView *) mapView:(MKMapView *)mapingView viewForAnnotation:(id <MKAnnotation>) annotation {
annView = nil;
if(annotation != mapingView.userLocation)
{
static NSString *defaultPinID = @"";
annView = (MKAnnotationView *)[mapingView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( annView == nil )
annView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] ;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = rightButton;
annView.canShowCallout = YES;
}
return annView;
}