帮助处理mapkit注释的多个图像

时间:2011-05-03 14:30:02

标签: annotations mapkit

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

#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

1 个答案:

答案 0 :(得分: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类没有像animatesDrop这样的MKPinAnnotationView属性,因此注释不会在地图上删除。如果需要投影动画,则必须手动完成(例如,在didAddAnnotationViews中)。