MKPinAnnotationView 的不同颜色取决于时间
Different colour of MKPinAnnotationColor dependant on time
我是编码新手,大约有 4 周的经验,所以请放轻松。
我基本上想在指定时间在地图上显示一个绿色图钉,当不是这些时间时,将显示一个红色图钉。
目前,用于设置两个不同图钉在指定时间出现的代码正在运行,但是我无法为每个实例实现不同颜色的图钉。
Annotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface Annotation : NSObject <MKAnnotation> {
}
@property(nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *subtitle;
@end
Annotation.m
#import "Annotation.h"
@implementation Annotation
@synthesize coordinate,title,subtitle;
@end
MapViewController.h
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface MapViewController : UITableViewController <MKMapViewDelegate, CLLocationManagerDelegate, MKAnnotation> {
IBOutlet UILabel *currentDate;
NSString *todaysDate;
MKMapView *mapView;
}
@property (strong,nonatomic) IBOutlet MKMapView *mapView;
@end
MapViewController.m
#import "MapViewController.h"
#import "SWRevealViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "Annotation.h"
@interface MapViewController ()
@end
@implementation MapViewController {
}
@synthesize mapView;
- (void)viewDidLoad {
self.mapView.delegate = self;
NSLocale* locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setLocale:locale];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"EEEE HH:mm"];
todaysDate = [formatter stringFromDate:[NSDate date]];
currentDate.text = todaysDate;
[super viewDidLoad];
//Annotations
NSMutableArray * locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
Annotation *myAnn;
//Area
//Venue
if (
//Mon
[todaysDate containsString: @"Monday 17"] || [todaysDate containsString: @"Monday 18"] ||
//Tues
[todaysDate containsString: @"Tuesday 17"] || [todaysDate containsString: @"Tuesday 18"] ||
//Weds
[todaysDate containsString: @"Wednesday 17"] || [todaysDate containsString: @"Wednesday 18"] ||
//Thurs
[todaysDate containsString: @"Thursday 17"] || [todaysDate containsString: @"Thursday 18"] ||
//Fri
[todaysDate containsString: @"Friday 17"] || [todaysDate containsString: @"Friday 18"] ||
//Sat
[todaysDate containsString: @"Saturday 17"] || [todaysDate containsString: @"Saturday 18"] ||
//Sun
[todaysDate containsString: @"Sunday 17"] || [todaysDate containsString: @"Sunday 18"]
)
{
//Green
myAnn = [[Annotation alloc] init];
location.latitude = 51.4;
location.longitude = -0.1;
myAnn.coordinate = location;
myAnn.title = @"Venue 1";
myAnn.subtitle = @"Subtitle 1";
[locations addObject:myAnn];
[self.mapView addAnnotations:locations];
//Green
} else {
//Red
myAnn = [[Annotation alloc] init];
location.latitude = 51.4;
location.longitude = -0.1;
myAnn.coordinate = location;
myAnn.title = @"Venue 1";
myAnn.subtitle = @"Subtitle 1";
[locations addObject:myAnn];
[self.mapView addAnnotations:locations];
//Red
}
@end
我知道我必须使用(在MapViewController.m):
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
但后来就迷路了...我试过将第二个引脚重命名为 myAnn 以外的名称,并根据名称设置 MKPinAnnotationColorRed 或 MKPinAnnotationColorGreen,但无法弄清楚。
如果有人能对此有所说明,我将不胜感激。
伙计们干杯
在 MKMapView Delegate “viewForAnnotation” 中,您需要使用以下方法创建一个新的 Pin,然后可以相应地更改颜色。
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"pizza_slice_32.png"];
pinView.pinColor = MKPinAnnotationColorPurple;
两个能够在两个注释之间有所不同,你应该在注释 class 上有一个特殊的 属性 .. 例如一个标签
//Green
myAnn = [[Annotation alloc] init];
myAnn.tag = 0;
location.latitude = 51.4;
location.longitude = -0.1;
myAnn.coordinate = location;
myAnn.title = @"Venue 1";
myAnn.subtitle = @"Subtitle 1";
[locations addObject:myAnn];
[self.mapView addAnnotations:locations];
//Green
} else {
//Red
myAnn = [[Annotation alloc] init];
myAnn.tag = 1;
location.latitude = 51.4;
location.longitude = -0.1;
myAnn.coordinate = location;
myAnn.title = @"Venue 1";
myAnn.subtitle = @"Subtitle 1";
[locations addObject:myAnn];
[self.mapView addAnnotations:locations];
//Red
}
然后打开标签
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
//only handle our Annotations
if(![annotation isKindOfClass:[Annotation class]])
return;
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"pizza_slice_32.png"];
pinView.pinColor = MKPinAnnotationColorPurple;
if((Annotation.tag == 0)) {
pinView.pinColor = MKPinAnnotationColorGreen;
}
else {
pinView.pinColor = MKPinAnnotationColorRed;
}
return pin;
}
我是编码新手,大约有 4 周的经验,所以请放轻松。
我基本上想在指定时间在地图上显示一个绿色图钉,当不是这些时间时,将显示一个红色图钉。
目前,用于设置两个不同图钉在指定时间出现的代码正在运行,但是我无法为每个实例实现不同颜色的图钉。
Annotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface Annotation : NSObject <MKAnnotation> {
}
@property(nonatomic, assign) CLLocationCoordinate2D coordinate;
@property(nonatomic, copy) NSString *title;
@property(nonatomic, copy) NSString *subtitle;
@end
Annotation.m
#import "Annotation.h"
@implementation Annotation
@synthesize coordinate,title,subtitle;
@end
MapViewController.h
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface MapViewController : UITableViewController <MKMapViewDelegate, CLLocationManagerDelegate, MKAnnotation> {
IBOutlet UILabel *currentDate;
NSString *todaysDate;
MKMapView *mapView;
}
@property (strong,nonatomic) IBOutlet MKMapView *mapView;
@end
MapViewController.m
#import "MapViewController.h"
#import "SWRevealViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "Annotation.h"
@interface MapViewController ()
@end
@implementation MapViewController {
}
@synthesize mapView;
- (void)viewDidLoad {
self.mapView.delegate = self;
NSLocale* locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setLocale:locale];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"EEEE HH:mm"];
todaysDate = [formatter stringFromDate:[NSDate date]];
currentDate.text = todaysDate;
[super viewDidLoad];
//Annotations
NSMutableArray * locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
Annotation *myAnn;
//Area
//Venue
if (
//Mon
[todaysDate containsString: @"Monday 17"] || [todaysDate containsString: @"Monday 18"] ||
//Tues
[todaysDate containsString: @"Tuesday 17"] || [todaysDate containsString: @"Tuesday 18"] ||
//Weds
[todaysDate containsString: @"Wednesday 17"] || [todaysDate containsString: @"Wednesday 18"] ||
//Thurs
[todaysDate containsString: @"Thursday 17"] || [todaysDate containsString: @"Thursday 18"] ||
//Fri
[todaysDate containsString: @"Friday 17"] || [todaysDate containsString: @"Friday 18"] ||
//Sat
[todaysDate containsString: @"Saturday 17"] || [todaysDate containsString: @"Saturday 18"] ||
//Sun
[todaysDate containsString: @"Sunday 17"] || [todaysDate containsString: @"Sunday 18"]
)
{
//Green
myAnn = [[Annotation alloc] init];
location.latitude = 51.4;
location.longitude = -0.1;
myAnn.coordinate = location;
myAnn.title = @"Venue 1";
myAnn.subtitle = @"Subtitle 1";
[locations addObject:myAnn];
[self.mapView addAnnotations:locations];
//Green
} else {
//Red
myAnn = [[Annotation alloc] init];
location.latitude = 51.4;
location.longitude = -0.1;
myAnn.coordinate = location;
myAnn.title = @"Venue 1";
myAnn.subtitle = @"Subtitle 1";
[locations addObject:myAnn];
[self.mapView addAnnotations:locations];
//Red
}
@end
我知道我必须使用(在MapViewController.m):
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
但后来就迷路了...我试过将第二个引脚重命名为 myAnn 以外的名称,并根据名称设置 MKPinAnnotationColorRed 或 MKPinAnnotationColorGreen,但无法弄清楚。
如果有人能对此有所说明,我将不胜感激。
伙计们干杯
在 MKMapView Delegate “viewForAnnotation” 中,您需要使用以下方法创建一个新的 Pin,然后可以相应地更改颜色。
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"pizza_slice_32.png"];
pinView.pinColor = MKPinAnnotationColorPurple;
两个能够在两个注释之间有所不同,你应该在注释 class 上有一个特殊的 属性 .. 例如一个标签
//Green
myAnn = [[Annotation alloc] init];
myAnn.tag = 0;
location.latitude = 51.4;
location.longitude = -0.1;
myAnn.coordinate = location;
myAnn.title = @"Venue 1";
myAnn.subtitle = @"Subtitle 1";
[locations addObject:myAnn];
[self.mapView addAnnotations:locations];
//Green
} else {
//Red
myAnn = [[Annotation alloc] init];
myAnn.tag = 1;
location.latitude = 51.4;
location.longitude = -0.1;
myAnn.coordinate = location;
myAnn.title = @"Venue 1";
myAnn.subtitle = @"Subtitle 1";
[locations addObject:myAnn];
[self.mapView addAnnotations:locations];
//Red
}
然后打开标签
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
//only handle our Annotations
if(![annotation isKindOfClass:[Annotation class]])
return;
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:@"pizza_slice_32.png"];
pinView.pinColor = MKPinAnnotationColorPurple;
if((Annotation.tag == 0)) {
pinView.pinColor = MKPinAnnotationColorGreen;
}
else {
pinView.pinColor = MKPinAnnotationColorRed;
}
return pin;
}