我是Xcode的新手,基本上我有一个MKMapView
,并希望为每个引脚添加单独的颜色。我目前标有带注释的引脚。
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文件有问题。我还想为此添加颜色引脚。如果新代码与我以前的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
答案 0 :(得分: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;
}