与MapKit帮助三个注释有三种不同颜色的脚 [英] Help with MapKit three annotations with three different pin colors

查看:129
本文介绍了与MapKit帮助三个注释有三种不同颜色的脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的应用程序开发和学习,我去(不是大家!)我能在地图上显示多个注释,但我想这三个引脚为三种不同的颜色,而不是都是一个颜色,我完全失去了。我完全MapViewController.m code以下。救命啊!

 #进口MapViewController.h@interface AddressAnnotation:NSObject的< MKAnnotation> {
    CLLocationCoordinate2D协调;
    * NSString的mTitle;
    * NSString的mSubTitle;
}
@结束@implementation AddressAnnotation@synthesize协调; - (的NSString *){字幕
    返回mSubTitle;
} - (的NSString *){称号
    返回mTitle;
} - (ID)initWithCoordinate:(CLLocationCoordinate2D)c标题:(* NSString的)标题字幕:(* NSString的)字幕{
    协调= C;
    mTitle = [标题保留]
    mSubTitle = [字幕保留]
    的NSLog(@%F,%F,c.latitude,c.longitude);
    返回自我;
}
- (无效){的dealloc
    [超级的dealloc];
    [mTitle发布]
    [mSubTitle发布]
}
@结束@implementation的MapViewController//指定初始化。如果您以编程方式创建控制器和要执行的定制是不适合的viewDidLoad覆盖。
/ *
  - (ID)initWithNibName:(* NSString的)nibNameOrNil包(一个NSBundle *)nibBundleOrNil {
 自= [超级initWithNibName:nibNameOrNil包:nibBundleOrNil];
 如果(个体经营){
 //定制的初始化。
 }
 返回自我;
 }
 * /
//实现viewDidLoad中加载的观点,通常从笔尖后做额外的设置。
- (无效)viewDidLoad中{
    [超级viewDidLoad中];    // ------设置地图的中心------
    CLLocationCoordinate2D中心;
    center.latitude = 37.83792;
    center.longitude = -122.247865;
    MKCoordinateRegion区域;
    MKCoordinateSpan跨度;
    span.latitudeDelta = 0.05;
    span.longitudeDelta = 0.05;
    region.center =中心;
    region.span =跨度;
    [图形页面setRegion:地区动画:是];    // ------要添加兴趣点------
    CLLocationCoordinate2D C1;
    //第一点
    c1.latitude = 37.8393624;
    c1.longitude = -122.2436549;
    AddressAnnotation * AD1 = [[AddressAnnotation页头] initWithCoordinate:C1标题:@标题在这里字幕:@字幕这里];
    [图形页面addAnnotation:AD1];
    [AD1发布]
    //第二点
    c1.latitude = 37.835964;
    c1.longitude = -122.250538;
    AddressAnnotation * AD2 = [[AddressAnnotation页头] initWithCoordinate:C1标题:@标题在这里字幕:@字幕这里];
    [图形页面addAnnotation:AD2];
    [AD2发布]
    //观点三
    c1.latitude = 37.8317039;
    c1.longitude = -122.2454169;
    AddressAnnotation * AD3 = [[AddressAnnotation页头] initWithCoordinate:C1标题:@标题在这里字幕:@字幕这里];
    [图形页面addAnnotation:AD3];
    [AD3发布]    // ----------------------------------------
} - (MKAnnotationView *)的MapView:(*的MKMapView)theMapView viewForAnnotation:(ID< MKAnnotation>)注释
{
    //如果是用户的位置,就返回nil。
    如果([注解isKindOfClass:[MKUserLocation类])
        回零;    MKPinAnnotationView * annView = [[MKPinAnnotationView页头] initWithAnnotation:注释reuseIdentifier:@MyPin];
    annView.rightCalloutAccessoryView = [UIButton的buttonWithType:UIButtonTypeDetailDisclosure];
    annView.animatesDrop = TRUE;
    annView.canShowCallout = YES;
    [annView的setSelected:YES];
    annView.pinColor = MKPinAnnotationColorPurple;
    annView.calloutOffset = CGPointMake(-2,2);
    返回annView;
}/ *
 //覆盖,让比默认纵向其他方向。
  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 //返回YES支持的方向。
 返回(interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 * / - (空)didReceiveMemoryWarning {
    //发布视图,如果它不具有一个上海华。
    [超级didReceiveMemoryWarning];    //释放任何缓存数据,图像等不在使用。
} - (无效){viewDidUnload
    [超级viewDidUnload]
    //释放任何保留的主视图的子视图。
    //例如self.myOutlet =零;
}
- (无效){的dealloc
    [超级的dealloc];
}
@结束


解决方案

一pinColor属性添加到 AddressAnnotation ,设置pinColor当您创建注释对象,然后设置色彩的 MKPinAnnotationView 根据颜色的 AddressAnnotation

  @interface AddressAnnotation:NSObject的< MKAnnotation> {
    /*...*/
    MKPinAnnotationColor pinColor;
}// viewDidLoad中
AddressAnnotation * AD1 = [[AddressAnnotation页头] initWithCoordinate:C1标题:@标题在这里字幕:@字幕这里];
ad1.pinColor = MKPinAnnotationColorGreen;//图形页面:viewForAnnotation:
annView.pinColor = annotation.pinColor;


或者你可以使用这样的事情,而不是 annView.pinColor = MKPinAnnotationColorPurple;

 静态NSInteger的pinColorCount = 0;
pinColorCount ++;
如果(pinColorCount == 1){
    annView.pinColor = MKPinAnnotationColorPurple;
}
否则如果(pinColorCount == 2){
    annView.pinColor = MKPinAnnotationColorRed;
}
否则如果(pinColorCount == 3){
    annView.pinColor = MKPinAnnotationColorGreen;
    pinColorCount = 0;
}

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

解决方案

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

这篇关于与MapKit帮助三个注释有三种不同颜色的脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆