gpt4 book ai didi

ios - 帮助 MapKit 使用三种不同的引脚颜色的三个注释

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:26:41 31 4
gpt4 key购买 nike

我对应用程序开发和学习还很陌生(我们不都是这样吗!)我可以在 map 上显示多个注释,但我希望三个图钉是三种不同的颜色,而不是全部是一种颜色,我完全迷路了。下面是我完整的 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

最佳答案

AddressAnnotation添加一个pinColor属性,在创建注释对象时设置pinColor,然后根据的颜色设置MKPinAnnotationView的颜色地址注释

@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;
}

关于ios - 帮助 MapKit 使用三种不同的引脚颜色的三个注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5861686/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com