2011-05-03 65 views
0

xcode很新,有些困惑。我能够使用自定义图像进行注释 - 工作正常。问题是我想要做的是为每个注释拥有不同的图像。我应该添加/更改下面的代码?提前感谢并记住,我是一名新手!有关mapkit注释的多个图像的帮助

#import "MapViewController.h" 

@interface AddressAnnotation : NSObject<MKAnnotation> { 
CLLocationCoordinate2D coordinate; 
NSString *mTitle; 
NSString *mSubTitle; 
UIImage *image; 

} 

@property (nonatomic, retain) UIImage *image; 

@end 

@implementation AddressAnnotation 


@synthesize coordinate; 
@synthesize image; 


- (NSString *)subtitle{ 
return mSubTitle; 
} 

- (NSString *)title{ 
return mTitle; 
} 


-(id)initWithCoordinate:(CLLocationCoordinate2D) c Title: (NSString *)title SubTitle: (NSString *) subTitle{ 
coordinate=c; 
mTitle = [title retain]; 
mSubTitle = [subTitle retain]; 
NSLog(@"%f,%f",c.latitude,c.longitude); 
return self; 
} 
-(void) dealloc{ 
[super dealloc]; 
[mTitle release]; 
[mSubTitle release]; 
} 
@end 

@implementation MapViewController 

// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. 
/* 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
// Custom initialization. 
} 
return self; 
} 
*/ 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
[super viewDidLoad]; 


//------ To Set center of the map ------ 
CLLocationCoordinate2D center; 
center.latitude = 37.83792; 
center.longitude = -122.247865; 
MKCoordinateRegion region; 
MKCoordinateSpan span; 
span.latitudeDelta = 0.05; 
span.longitudeDelta = 0.05; 
region.center = center; 
region.span = span; 
[mapView setRegion:region animated:YES]; 

//------ To Add a point of interest ------ 
CLLocationCoordinate2D c1; 
// Point one 
c1.latitude = 37.8393624; 
c1.longitude = -122.2436549; 
AddressAnnotation* ad1 = [[AddressAnnotation alloc] initWithCoordinate:c1 Title:@"title here" SubTitle:@"subtitle here"]; 
ad1.image = [UIImage imageNamed:@"img01.png"]; 
[mapView addAnnotation:ad1]; 
[ad1 release]; 
// Point two 
c1.latitude = 37.835964; 
c1.longitude = -122.250538; 
AddressAnnotation* ad2 = [[AddressAnnotation alloc] initWithCoordinate:c1 Title:@"title here" SubTitle:@"subtitle here"]; 
ad2.image = [UIImage imageNamed:@"img02.png"]; 
[mapView addAnnotation:ad2]; 
[ad2 release]; 
// Point three 
c1.latitude = 37.8317039; 
c1.longitude = -122.2454169; 
AddressAnnotation* ad3 = [[AddressAnnotation alloc] initWithCoordinate:c1 Title:@"title here" SubTitle:@"subtitle here"]; 
ad3.image = [UIImage imageNamed:@"img03.png"]; 
[mapView addAnnotation:ad3]; 
[ad3 release]; 

//---------------------------------------- 
} 

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
if ([annotation isKindOfClass:[AddressAnnotation class]]) 
{ 
    static NSString *AnnotationIdentifier = @"AnnotationIdentifier"; 
    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier]; 
    if (!pinView) 
    { 
     pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease]; 
     pinView.canShowCallout = YES; 
     pinView.animatesDrop = YES; 
    } 
    else 
    { 
     pinView.annotation = annotation; 
    } 

    UIImageView *leftCalloutView = [[UIImageView alloc] 
            initWithImage:((AddressAnnotation *)annotation).image]; 
    pinView.leftCalloutAccessoryView = leftCalloutView; 
    [leftCalloutView release]; 

    return pinView; 
} 

return nil; 
} 
/* 
// Override to allow orientations other than the default portrait orientation. 
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
// Return YES for supported orientations. 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
[super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc. that aren't in use. 
    } 

- (void)viewDidUnload { 
[super viewDidUnload]; 
// Release any retained subviews of the main view. 
// e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
[super dealloc]; 
} 
@end 
+0

你应该能够适应你的问题[这个答案](http://stackoverflow.com/questions/4841753/custom-mkannotation-class-for-changing-leftcalloutannotationview/4843134#4843134)。 – Anna 2011-05-03 15:21:05

+0

你好安娜K!谢谢你的帮助。在玩了一段时间之后,我才得以开始工作......有点。标注全部有不同的图像,但注释引脚是红色的引脚,我想要有图像。我知道我错过了一些明显的东西。非常感谢您的帮助。 – fraga 2011-05-03 18:33:47

+0

上面的修改代码 – fraga 2011-05-03 18:50:02

回答

2

要显示自己的图像,而不是标准的引脚,创建一个纯MKAnnotationView,而不是MKPinAnnotationView并设置其image属性而不是leftCalloutAccessoryView的:

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    if ([annotation isKindOfClass:[AddressAnnotation class]]) 
    { 
     static NSString *AnnotationIdentifier = @"AnnotationIdentifier"; 
     MKAnnotationView *pinView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier]; 
     if (!pinView) 
     { 
      pinView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease]; 
      pinView.canShowCallout = YES; 
     } 
     else 
     { 
      pinView.annotation = annotation; 
     } 

     pinView.image = ((AddressAnnotation *)annotation).image; 

     return pinView; 
    } 

    return nil; 
} 

注意,MKAnnotationView类没有按” t有一个animatesDrop属性,如MKPinAnnotationView,因此注释不会在地图上放下。如果需要拖放动画,则必须手动完成(例如,在didAddAnnotationViews中)。

+0

你是上帝!非常感谢。如果你来到坎昆地区,让我知道 - 我欠你一杯啤酒! – fraga 2011-05-03 19:25:16