iOS:为什么我不能让 mapkit 显示自定义注释图钉图像? [英] iOS : Why can't I get mapkit to display a custom annotation pin image?
问题描述
认为使用我自己的自定义图钉图像进行注释会非常容易.
figured that using my own custom pin image for annotations would be super easy.
但我一直无法让它工作,我不知道为什么!
But I have never been able to get it to work, and I have no idea why!
我只是在使用:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKUserLocation class]])
return nil;
NSString *annotationIdentifier = @"CustomViewAnnotation";
CustomAnnotationView * customAnnotationView = (CustomAnnotationView *) [self.mapview dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if(!customAnnotationView)
{
customAnnotationView=[[CustomAnnotationView alloc] initWithAnnotationWithImage:annotation
reuseIdentifier:annotationIdentifier
annotationViewImage:[UIImage imageNamed:@"map_location_pin.png"]];
}
customAnnotationView.image=[UIImage imageNamed:@"map_location_pin.png"];
customAnnotationView.canShowCallout= YES;
return customAnnotationView;
}
我认为这应该可行,因为 UIImage 正是它想要的,而且我的项目中确实有 .png 文件.
I assumed that this should work, as a UIImage is what it is wanting, and I do have the .png file in my project.
它从不使用我的图像,而只是使用标准的红色图钉.我在这里做错了什么?
It never uses my image, but just the standard red pin. What am I doing wrong here?
推荐答案
一些想法:
您是否为
MKMapView
定义了delegate
?您是否在此处放置了断点或NSLog
以确保您的viewForAnnotation
被调用?
Did you define your
delegate
for yourMKMapView
? Have you put a breakpoint orNSLog
in here to make sure yourviewForAnnotation
is getting called?
你还没有分享你的 CustomAnnotationView
接口/实现细节,但你不需要子类化 MKAnnotationView
除非你在那里做一些特别的事情.如果你只是替换图像,你可以这样做:
You haven't shared your CustomAnnotationView
interface/implementation details, but you don't need to subclass MKAnnotationView
unless you're doing something special in there. If you're just replacing the image, you can just do:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
NSString *annotationIdentifier = @"CustomViewAnnotation";
MKAnnotationView* annotationView = [mapview dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
if (annotationView) {
annotationView.annotation = annotation;
} else {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
}
annotationView.image = [UIImage imageNamed:@"map_location_pin.png"];
annotationView.canShowCallout= YES;
return annotationView;
}
显然,如果您正在做一些有趣的事情(例如拖放引脚时的自定义注释),请继续使用您的 CustomAnnotationView
子类 MKAnnotationView
.这取决于你想要做什么.
Clearly, though, if you're doing something interesting (like custom annotation when a pin is dragged or dropped), then go ahead and have your CustomAnnotationView
subclass MKAnnotationView
. It just depends upon what you're trying to do.
与您最初的问题无关,我建议:
Unrelated to your original question, I'd suggest:
您的
viewForAnnotation
方法仅使用本地mapView
变量而不是引用类属性self.mapView
(尽管它们'毫无疑问是一样的),
that your
viewForAnnotation
method just use the localmapView
variable rather than referencing a class property,self.mapView
(though they're undoubtedly the same),
您考虑重命名自定义 init 方法.所以,而不是:
that you contemplate renaming your custom init method. So instead of:
customAnnotationView = [[CustomAnnotationView alloc] initWithAnnotationWithImage:annotation
reuseIdentifier:annotationIdentifier
annotationViewImage:[UIImage imageNamed:@"map_location_pin.png"]];
你会做这样的事情:
customAnnotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:annotationIdentifier
image:[UIImage imageNamed:@"map_location_pin.png"]];
如果您仍然遇到问题,请与我们分享您的一些 CustomAnnotationView
代码.
If you're still having troubles, share some of your CustomAnnotationView
code with us.
这篇关于iOS:为什么我不能让 mapkit 显示自定义注释图钉图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!