我的MKPointAnnotation应该使用此代码进行自定义:
-(MKPointAnnotation*)setAnnotation: (NSString*) title atLocation:(CLLocationCoordinate2D)Location withImage:(UIImage*) LocationImage{
Pin = [[MKPointAnnotation alloc] init];
Pin.title = title;
[Pin setCoordinate:Location];
[self mapView:mapView viewForAnnotation:Pin].annotation = Pin;
return Pin;
}
-(MKAnnotationView *)mapView: (MKMapView *)mapView viewForAnnotation: (id<MKAnnotation>) annotation{
MKPinAnnotationView* pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
if(!pinView){
pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
pinView.enabled = YES;
UIButton *PicButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[PicButton addTarget:self action:@selector(showLocationPicture) forControlEvents:UIControlEventTouchUpInside];\
pinView.rightCalloutAccessoryView = PicButton;
}
else{
pinView.annotation = annotation;
}
return pinView;
}
然而,出于某种原因,Pin仍然是默认的,任何人都可以帮助我吗? 感谢
答案 0 :(得分:10)
您不应该自己致电viewForAnnotation
。您只应在地图视图中添加注释,然后确定在任何给定时刻可见的注释,并为您调用viewForAnnotation
。因此:
鉴于不同的注释可以有不同的图像,你真的想要子类MKPointAnnotation
并给它一个imageName
的属性(注意,而不是UIImage
本身,但是viewForAnnotation
可用于创建UIImage
本身的名称或标识符:
@interface MyAnnotation : MKPointAnnotation
@property(nonatomic, strong) NSString *imageName;
@end
@implementation MyAnnotation
@end
向地图视图添加注释(不是注释视图),例如:
- (id<annotation>)addAnnotationWithTitle:(NSString *)title coordinate:(CLLocationCoordinate2D)coordinate imageName:(NSString *)imageName
{
MyAnnotation *annotation = [[MyAnnotation alloc] init];
annotation.title = title;
annotation.coordinate = coordinate;
annotation.imageName = imageName;
[mapView addAnnotation:annotation];
return annotation;
}
注意,我不建议将Pin
作为类ivar / property,并且我将camelCase用于变量名(以小写开头),我将从setAnnotation
更改方法名称类似于addAnnotationWithTitle
等
确保您的控制器已被指定为mapView的委托;
将为您调用viewForAnnotation
(如果注释可见)。它可能应该是这样的:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
if ([annotation isKindOfClass:[MyAnnotation class]])
{
MyAnnotation *customAnnotation = annotation;
static NSString *annotationIdentifier = @"MyAnnotation";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (!annotationView)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
else
{
annotationView.annotation = annotation;
}
annotationView.image = [UIImage imageNamed:customAnnotation.imageName];
return annotationView;
}
return nil;
}
注意,animatesDrop
是MKPinAnnotationView
选项,您无法使用自定义注释视图。但如果你愿意,可以很容易地实现一个(参见How can I create a custom "pin-drop" animation using MKAnnotationView?)。我建议你首先解决基本的注释视图,然后再看看自定义注释视图投影动画。