2013-04-08 56 views
1

我是Xcode的新手,基本上我有一個MKMapView,並且希望爲我的每個引腳添加單獨的顏色。我目前有註釋的標記引腳。MKMap引腳顏色

LocationAnnotation.h:

@interface LocationAnnotation : NSObject <MKAnnotation> 

@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy) NSString *title; 
@property (nonatomic, copy) NSString *subtitle; 

@end 

LocationAnnotation.m:

#import "LocationAnnotation.h" 

@implementation LocationAnnotation 
@synthesize coordinate, title, subtitle; 

@end 

我的每一個座標,像這樣在 MainMapViewController.h:

#define TheKingBill_LATITUDE 50.431379 
#define TheKingBill_LONGITUDE -3.685495 

//Annotation 

NSMutableArray * locations = [[NSMutableArray alloc] init]; 
CLLocationCoordinate2D location; 
LocationAnnotation * myAnn; 

//The King Bill Annotation 
myAnn = [[LocationAnnotation alloc] init]; 
location.latitude = TheKingBill_LATITUDE; 
location.longitude = TheKingBill_LONGITUDE; 
myAnn.coordinate = location; 
myAnn.title = @"The King Bill"; 
myAnn.subtitle = @"Another Pub In Town"; 
[locations addObject:myAnn]; 

//The Seven Stars Annotations 
myAnn = [[LocationAnnotation alloc] init]; 
location.latitude = TheSevenStars_LATITUDE; 
location.longitude = TheSevenStars_LONGITUDE; 
myAnn.coordinate = location; 
myAnn.title = @"The Royal Seven Stars Hotel"; 
myAnn.subtitle = @"Hotel In Town"; 
[locations addObject:myAnn]; 

[self.MainMapView addAnnotations:locations]; 

任何想法上我需要添加什麼來使每個引腳顏色不同?

謝謝!


好吧,現在我有一個問題,我的其他地圖鏈接到這個LocationAnnotation文件。我也想添加顏色的針腳。如果新代碼與我以前的mapview控制器相同,這將是理想的。下面是代碼...

LocationAnnotation.h

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 
#import <CoreLocation/CoreLocation.h> 

@interface LocationAnnotation : NSObject <MKAnnotation> 

@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy) NSString *title; 
@property (nonatomic, copy) NSString *subtitle; 
@property int idNumber; 

- (id)initWithTitle:(NSString *)ttl andSubtitle:(NSString *)subttl andCoordinate:(CLLocationCoordinate2D)c2d andID:(int)idN; 

@end 

LocationAnnotation.m

#import "LocationAnnotation.h" 

    @implementation LocationAnnotation 

    @synthesize coordinate, title, subtitle, idNumber; 

    - (id)initWithTitle:(NSString *)ttl andSubtitle:(NSString *)subttl andCoordinate:(CLLocationCoordinate2D)c2d andID:(int)idN 
    { 
     self = [super init]; 

     if (self) 
     { 
      self.title = ttl; 
      self.coordinate = c2d; 
      self.subtitle = subttl; 
      self.idNumber = idN; 
     } 

     return self; 
    } 

    @end 

MapViewController.h

#import <UIKit/UIKit.h> 
    #import <MapKit/MapKit.h> 

    @interface MapViewController : UIViewController 

    @property (weak, nonatomic) IBOutlet MKMapView *ClientMapView; 

    @end 

MapViewController.m

#import "MapViewController.h" 
#import "LocationAnnotation.h" 

@interface MapViewController() 

@end 


//The Dartmouth Inn Coordinates 
#define DARTMOUTH_INN_LATITUDE 50.430036; 
#define DARTMOUTH_INN_LONGITUDE -3.683873; 

//Span 
#define THE_SPAN 0.004f; 



@implementation MapViewController 
@synthesize ClientMapView; 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    //Create the region 
    MKCoordinateRegion myRegion; 

    //Centre 
    CLLocationCoordinate2D centre; 
    centre.latitude = DARTMOUTH_INN_LATITUDE; 
    centre.longitude = DARTMOUTH_INN_LONGITUDE; 

    //Span 
    MKCoordinateSpan span; 
    span.latitudeDelta = THE_SPAN; 
    span.longitudeDelta = THE_SPAN; 

    myRegion.center = centre; 
    myRegion.span = span; 

    //Set The Map View 
    [ClientMapView setRegion:myRegion animated:YES]; 


    //Annotation 

    //1. Create A Coordinate for use with annotation 

    CLLocationCoordinate2D dartLocation; 
    dartLocation.latitude = DARTMOUTH_INN_LATITUDE; 
    dartLocation.longitude = DARTMOUTH_INN_LONGITUDE; 


    LocationAnnotation * myAnnotation = [LocationAnnotation alloc]; 
    myAnnotation.coordinate = dartLocation; 
    myAnnotation.title = @"The Dartmouth Inn"; 

    [self.ClientMapView addAnnotation:myAnnotation]; 




} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 
+0

我想單獨設置每個註釋以便我可以選擇它的顏色。有任何想法嗎? – 2013-04-08 12:37:16

回答

1

好了,我們在這裏,你的代碼修改:

LocationAnnotation.h

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 

@interface LocationAnnotation : NSObject <MKAnnotation> 

@property (nonatomic, assign) CLLocationCoordinate2D coordinate; 
@property (nonatomic, copy) NSString *title; 
@property (nonatomic, copy) NSString *subtitle; 
@property int idNumber; 

- (id)initWithTitle:(NSString *)ttl andSubtitle:(NSString *)subttl andCoordinate:(CLLocationCoordinate2D)c2d andID:(int)idN; 

@end 

LocationAnnotation.m

#import "LocationAnnotation.h" 

@implementation LocationAnnotation 

@synthesize coordinate, title, subtitle, idNumber; 

- (id)initWithTitle:(NSString *)ttl andSubtitle:(NSString *)subttl andCoordinate:(CLLocationCoordinate2D)c2d andID:(int)idN 
{ 
    self = [super init]; 

    if (self) 
    { 
     self.title = ttl; 
     self.coordinate = c2d; 
     self.subtitle = subttl; 
     self.idNumber = idN; 
    } 

    return self; 
} 

@end 

MainMapViewController.h

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 

@interface MainMapViewController : UIViewController <MKMapViewDelegate> 

@property (weak, nonatomic) IBOutlet MKMapView *mainMapView; 

@end 

MainMapViewController.m

#import "MainMapViewController.h" 
#import "LocationAnnotation.h" 

@interface MainMapViewController() 

@end 

//Totnes Main Centre Coordinates 
#define Totnes_LATITUDE 50.433741 
#define Totnes_LONGITUDE -3.685797 

//The Dartmouth Inn Coordinates 
#define DARTMOUTH_INN_LATITUDE 50.430036; 
#define DARTMOUTH_INN_LONGITUDE -3.683873; 

//Pub Offers Co-Ordinates 

#define TheKingBill_LATITUDE 50.431379 
#define TheKingBill_LONGITUDE -3.685495 

#define TheSevenStars_LATITUDE 50.431045 
#define TheSevenStars_LONGITUDE -3.682945 

#define TheLordNelson_LATITUDE 50.430931 
#define TheLordNelson_LONGITUDE -3.683644 

//Span 
#define THE_SPAN 0.01f; 


@implementation MainMapViewController 

@synthesize mainMapView; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //Set Delegate 
    mainMapView.delegate = self; 

    //Create the region 
    MKCoordinateRegion myRegion; 

    //Centre 
    CLLocationCoordinate2D centre; 
    centre.latitude = Totnes_LATITUDE; 
    centre.longitude = Totnes_LONGITUDE; 

    //Span 
    MKCoordinateSpan span; 
    span.latitudeDelta = THE_SPAN; 
    span.longitudeDelta = THE_SPAN; 

    myRegion.center = centre; 
    myRegion.span = span; 


    //Set The Map View 
    [mainMapView setRegion:myRegion animated:YES]; 


    //Annotation 

    NSMutableArray * locations = [[NSMutableArray alloc] init]; 
    LocationAnnotation * myAnn; 

    //The King Bill Annotation 
    myAnn = [[LocationAnnotation alloc] initWithTitle:@"The King Bill" 
              andSubtitle:@"Another Pub In Town" 
             andCoordinate:CLLocationCoordinate2DMake(TheKingBill_LATITUDE, TheKingBill_LONGITUDE) 
               andID:1]; 
    [locations addObject:myAnn]; 

    //The Seven Stars Annotations 
    myAnn = [[LocationAnnotation alloc] initWithTitle:@"The Royal Seven Stars Hotel" 
              andSubtitle:@"Hotel In Town" 
             andCoordinate:CLLocationCoordinate2DMake(TheSevenStars_LATITUDE, TheSevenStars_LONGITUDE) 
               andID:2]; 
    [locations addObject:myAnn]; 

    //The Lord Nelson Annotations 
    myAnn = [[LocationAnnotation alloc] initWithTitle:@"The Lord Nelson" 
              andSubtitle:@"Great Pub In Centre of Town" 
             andCoordinate:CLLocationCoordinate2DMake(TheLordNelson_LATITUDE, TheLordNelson_LONGITUDE) 
               andID:3]; 
    [locations addObject:myAnn]; 

    [self.mainMapView addAnnotations:locations]; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


#pragma mark - MKMapViewDelegate 

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation { 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 

    MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"]; 

    int annId = ((LocationAnnotation *)annotation).idNumber; 
    annView.pinColor = (annId == 1) ? MKPinAnnotationColorPurple 
            : (annId == 2) ? MKPinAnnotationColorGreen 
                : MKPinAnnotationColorRed; 
    annView.canShowCallout = YES; 
    return annView; 
} 
+0

我把這個放在我的LocationAnnotation.m中嗎? – 2013-04-08 12:10:40

+0

我想單獨設置每個註釋,以便我可以選擇它的顏色。 – 2013-04-08 12:36:48

+0

但是,我應該如何處理我目前擁有的所有其他信息,例如標題,副標題,經度和緯度。 Idealy我想只將代碼添加到我擁有的當前代碼中。有沒有辦法做到這一點,並添加另一個屬性? – 2013-04-08 16:12:23