我是 Xcode 的新手,基本上我有一个MKMapView
并且想为我的每个引脚添加单独的颜色。我目前已经用注释标记了别针。
位置注释.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 文件。我还想为此添加颜色图钉。如果新代码与我以前的 mapviewcontroller 相同,那将是理想的。这是代码...
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