iOS:为什么可以';我不能让mapkit显示自定义注释pin图像吗?
我想使用我自己的自定义pin图像进行注释会非常简单 但我一直没能让它工作,我也不知道为什么 我只是使用:iOS:为什么可以';我不能让mapkit显示自定义注释pin图像吗?,ios,annotations,mkmapview,mapkit,Ios,Annotations,Mkmapview,Mapkit,我想使用我自己的自定义pin图像进行注释会非常简单 但我一直没能让它工作,我也不知道为什么 我只是使用: - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { if([annotation isKindOfClass:[MKUserLocation class]]) return nil; NSString
- (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;
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
if([annotation isKindOfClass:[MKUserLocation类]])
返回零;
NSString*annotationIdentifier=@“CustomViewAnnotation”;
CustomAnnotationView*CustomAnnotationView=(CustomAnnotationView*)[self.mapview出列可重用AnnotationViewWithIdentifier:annotationIdentifier];
如果(!customAnnotationView)
{
customAnnotationView=[[customAnnotationView alloc]initWithAnnotationWithImage:注释
reuseIdentifier:annotationIdentifier
annotationViewImage:[UIImage ImageName:@“map_location_pin.png”];
}
customAnnotationView.image=[UIImage ImageName:@“map\u location\u pin.png”];
customAnnotationView.canShowCallout=是;
返回customAnnotationView;
}
我认为这应该是可行的,因为UIImage正是它所需要的,而且我的项目中确实有.png文件
它从不使用我的图像,只使用标准的红色别针。我做错了什么?我想这是对你的支持。你能试试这段代码吗。你可以添加我的注释.h和.m文件,并对我的注释做一些更改 我的注解
#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;
}
#导入
#进口
@接口MyAnnotation:NSObject{
CLLocationCoordinate2D坐标;
NSString*标题;
NSString*副标题;
UIImage*图像;
}
@属性(非原子,赋值)CLLocationCoordinate2D坐标;
@属性(非原子,副本)NSString*标题;
@属性(非原子,复制)NSString*副标题;
@属性(非原子,保留)UIImage*image;
@结束
我的注解
#导入“MyAnnotation.h”
@实现MyAnnotation
@综合职称;
@综合字幕;
@综合坐标;
@合成图像;
-(无效)解除锁定
{
[super dealoc];
self.title=零;
self.subtitle=nil;
self.image=nil;
}
@结束
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
//NSLog(@“欢迎进入地图视图注释”);
//如果是用户位置,只需返回nil。
if([annotation isKindOfClass:[MKUserLocation类]])
返回零;
//首先尝试将现有pin视图出列
静态NSString*AnnotationIdentifier=@“AnnotationIdentifier”;
MKPinAnnotationView*pinView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:AnnotationIdentifier]autorelease];
//annotation.image=[UIImage ImageName:@“shop-1.png”];
//[pinView setImage:[UIImage ImageName:@“shop-1.png”];
UIImageView*thumbnailImageView=[[UIImageView alloc]initWithImage:((MyAnnotation*)annotation.image];
CGRect newBounds=CGRectMake(0.0,0.0,32.0,32.0);
[thumbnailImageView收进边界:新边界];
pinView.leftCalloutAccessoryView=thumbnailImageView;
[thumbnailImageView发布];
//UIImageView*profileIconView=[[UIImageView alloc]initWithImage:FeaturedDealCupOnImage1];
//[pinView设置图像:FeatureAddeAlCupOnImage1];
pinView.animatesDrop=否;
pinView.canShowCallout=是;
pinView.pinColor=MKPinAnnotationColorGreen;
UIButton*rightButton=[UIButton Button类型:UIButtonTypedTailButton];
[rightButton setTitle:annotation.title for状态:UIControlStateNormal];
[rightButton addTarget:self action:@selector(_annotation_btn_click:)for ControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView=rightButton;
返回pinView;
}
确保您正在子类化
NSObject <MKAnnotation>
有几点想法:
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 = [[CustomAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:annotationIdentifier
image:[UIImage imageNamed:@"map_location_pin.png"]];
CustomAnnotationView
代码。请参考以下内容:
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"]];