我的 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 仍然是默认的,有人可以帮助我吗?
谢谢
你不应该打电话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,我将使用驼峰命名法作为变量名称(以小写字母开头),我将更改方法名称setAnnotation
类似的东西addAnnotationWithTitle
, etc.
确保您的控制器已被指定为mapView的委托;
-
The 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
is a MKPinAnnotationView
这是您无法使用自定义注释视图执行的选项。但如果你愿意的话,实现起来很容易(参见如何使用 MKAnnotationView 创建自定义“针落”动画? https://stackoverflow.com/questions/1857160/how-can-i-create-a-custom-pin-drop-animation-using-mkannotationview/7045916#7045916)。我建议您首先处理基本注释视图,然后再查看自定义注释视图放置动画。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)