我正在尝试一些不同的东西。
我使用MKPinAnnotationView
和继承的图像实例而不是类。以下是viewForAnnotation
的代码。但它不起作用。什么都没有显示出来。
我也尝试使用MKAnnotationView
并得到相同的结果。
- (MKAnnotationView *)mapView:(MKMapView *)eMapView viewForAnnotation:(id <MKAnnotation>)annotation {
int postTag = 0;
MKAnnotationView *pinView = (MKAnnotationView*)[eMapView dequeueReusableAnnotationViewWithIdentifier:@"Pin"];
if(pinView == nil) {
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Pin"];
pinView.frame = CGRectMake(0, 0, 25, 25);
} else {
pinView.annotation = annotation;
}
pinView.image = [UIImage alloc];
NSString *data_annotation = [annotation getData];
if([data_annotation length] == 0) {
pinView.image = [UIImage imageNamed:@"pinYellow.png"];
// pinView.pinColor = MKPinAnnotationColorGreen;
}
else if(([[data_annotation rangeOfString:@"GRAY"].location != NSNotFound)){
pinView.image = [UIImage imageNamed:@"pinGray.png"];
// pinView.pinColor = MKPinAnnotationColorPurple;
}
else {
pinView.image = [UIImage imageNamed:@"pinRed.png"];
// pinView.pinColor = MKPinAnnotationColorRed;
}
// Set up the Right callout
UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
myDetailButton.frame = CGRectMake(0, 0, 23, 23);
myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[myDetailButton addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
// Identify which pin is being selected
if ([[annotation title] isEqualToString:@"Current Location"]) {
postTag = 99999;
} else {
postTag = [annotation getPinID];
}
myDetailButton.tag = postTag;
pinView.rightCalloutAccessoryView = myDetailButton;
//pinView.animatesDrop = YES;
// Set to show a callout on the pin
pinView.canShowCallout = YES;
return pinView;
}