问题
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!
I am simply using:
- (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;
}
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?
回答1:
A couple of thoughts:
Did you define your
delegate
for yourMKMapView
? Have you put a breakpoint orNSLog
in here to make sure yourviewForAnnotation
is getting called?You haven't shared your
CustomAnnotationView
interface/implementation details, but you don't need to subclassMKAnnotationView
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; }
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
subclassMKAnnotationView
. It just depends upon what you're trying to do.Unrelated to your original question, I'd suggest:
that your
viewForAnnotation
method just use the localmapView
variable rather than referencing a class property,self.mapView
(though they're undoubtedly the same),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"]];
that you'd do something like:
customAnnotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier image:[UIImage imageNamed:@"map_location_pin.png"]];
If you're still having troubles, share some of your CustomAnnotationView
code with us.
回答2:
I think It's support to you.can you try this code.and you can add my annotation .h and .m files and do some changes My annotataion
my annotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyAnnotation : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString* title;
NSString* subtitle;
UIImage* image;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString* title;
@property (nonatomic, copy) NSString* subtitle;
@property (nonatomic, retain) UIImage* image;
@end
my annotation.m
#import "MyAnnotation.h"
@implementation MyAnnotation
@synthesize title;
@synthesize subtitle;
@synthesize coordinate;
@synthesize image;
- (void)dealloc
{
[super dealloc];
self.title = nil;
self.subtitle = nil;
self.image=nil;
}
@end
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
//NSLog(@"welcome into the map view annotation");
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// try to dequeue an existing pin view first
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier]autorelease];
//annotation.image = [UIImage imageNamed:@"shop-1.png"];
//[pinView setImage:[UIImage imageNamed:@"shop-1.png"]];
UIImageView *thumbnailImageView = [[UIImageView alloc] initWithImage:((MyAnnotation *)annotation).image];
CGRect newBounds = CGRectMake(0.0, 0.0, 32.0, 32.0);
[thumbnailImageView setBounds:newBounds];
pinView.leftCalloutAccessoryView = thumbnailImageView;
[thumbnailImageView release];
//UIImageView *profileIconView = [[UIImageView alloc] initWithImage:featuredDealcouponImage1];
// [pinView setImage:featuredDealcouponImage1];
pinView.animatesDrop=NO;
pinView.canShowCallout=YES;
pinView.pinColor=MKPinAnnotationColorGreen;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self action:@selector(_annotation_btn_click:) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
return pinView;
}
回答3:
Make sure you are subclassing
NSObject <MKAnnotation>
and not
MKPinAnnotationView
来源:https://stackoverflow.com/questions/15362002/ios-why-cant-i-get-mapkit-to-display-a-custom-annotation-pin-image