帮助使用 MapKit 三个注释以及三种不同的图钉颜色

发布于 2024-11-04 15:09:47 字数 4019 浏览 1 评论 0原文

我对应用程序开发和学习非常陌生(不是吗!)我能够在地图上显示多个注释,但我希望三个图钉是三种不同的颜色,而不是全部一种颜色,我我完全迷路了。我的完整 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

I’m very new to app development and learning as I go (aren’t we all!) I'm able to show multiple annotations on the map BUT I want the three pins to be three different colors instead of all one color and I’m totally lost. My complete MapViewController.m code below. Help!

#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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜声 2024-11-11 15:09:47

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

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

Add a pinColor property to AddressAnnotation, set the pinColor when you create your annotation object and then set the color of the MKPinAnnotationView according to the color of the 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;

or you could use something like this instead of 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文