我对应用程序的开发和学习非常陌生(不是我们所有人!)我能够在地图上显示多个注释但我希望三个引脚是三种不同颜色而不是全部一种颜色,我完全失去了。我的完整MapViewController.m代码如下。救命啊!
#import "MapViewController.h"
@interface AddressAnnotation : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *mTitle;
NSString *mSubTitle;
}
@end
@implementation AddressAnnotation
@synthesize coordinate;
- (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"];
[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"];
[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"];
[mapView addAnnotation:ad3];
[ad3 release];
//----------------------------------------
}
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MyPin"];
annView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
[annView setSelected:YES];
annView.pinColor = MKPinAnnotationColorPurple;
annView.calloutOffset = CGPointMake(-2, 2);
return annView;
}
/*
// 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 :(得分:2)
将pinColor属性添加到AddressAnnotation
,在创建注释对象时设置pinColor,然后根据MKPinAnnotationView
AddressAnnotation
的颜色
@interface AddressAnnotation : NSObject<MKAnnotation> {
/*...*/
MKPinAnnotationColor pinColor;
}
// viewDidLoad
AddressAnnotation* ad1 = [[AddressAnnotation alloc] initWithCoordinate:c1 Title:@"Title here" SubTitle:@"subtitle here"];
ad1.pinColor = MKPinAnnotationColorGreen;
// mapView:viewForAnnotation:
annView.pinColor = annotation.pinColor;
或者你可以使用这样的东西代替annView.pinColor = MKPinAnnotationColorPurple;
static NSInteger pinColorCount = 0;
pinColorCount++;
if (pinColorCount == 1) {
annView.pinColor = MKPinAnnotationColorPurple;
}
else if (pinColorCount == 2) {
annView.pinColor = MKPinAnnotationColorRed;
}
else if (pinColorCount == 3) {
annView.pinColor = MKPinAnnotationColorGreen;
pinColorCount = 0;
}