认为使用我自己的自定义图钉进行注释非常容易。
但是我从来没有能够让它工作,我不知道为什么!
我只是在使用:
- (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文件。
它从不使用我的图像,只是标准的红色图钉。我在这里做错了什么?
答案 0 :(得分:6)
有几点想法:
您是否为delegate
定义了MKMapView
?您是否在此处设置了断点或NSLog
以确保您的viewForAnnotation
被调用?
您尚未共享CustomAnnotationView
接口/实现详细信息,但除非您在那里做了一些特别的事情,否则不需要继承MKAnnotationView
。如果你只是替换图像,你可以这样做:
- (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
。这取决于你想要做什么。
与您原来的问题无关,我建议:
您的viewForAnnotation
方法只使用本地mapView
变量,而不是引用类属性self.mapView
(尽管它们无疑是相同的),
您考虑重命名自定义init方法。所以而不是:
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
代码。
答案 1 :(得分:2)
我认为这是对你的支持。你可以尝试这段代码。你可以添加我的注释.h和.m文件并做一些修改我的注释
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;
}
答案 2 :(得分:0)
确保您是子类
NSObject <MKAnnotation>
而不是
MKPinAnnotationView