MKPinAnnotationView 的不同颜色取决于时间
Posted
技术标签:
【中文标题】MKPinAnnotationView 的不同颜色取决于时间【英文标题】:Different colour of MKPinAnnotationColor dependant on time 【发布时间】:2015-10-22 22:58:32 【问题描述】:我是编码新手,大约有 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
注解.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 但无法弄清楚。
如果有人能对此有所了解,我将不胜感激。
大家好
【问题讨论】:
请查看***.com/help/mcve。 【参考方案1】:两个注释可以不同,你应该在注释类上有一个特殊的属性..例如一个标签
//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;
【讨论】:
谢谢大吉,这正是我要找的。我得到“在'Annotation'类型的对象上找不到属性'标签'”。我想我必须在Annotation.h中设置一些东西并在Annotation.m中合成它。 @property(nonatomic, assign) X *tag; 这样的?不确定 X 值应该是多少。 好的,我已经设置了@property(nonatomic, assign) NSInteger 标签;但是我仍然收到错误“在'注释'类型的对象上找不到属性'标签'”,有什么想法吗? 哦,我的错。 ((Annotation*)annotation).tag :: typo ;) 必须将变量转换为 Annotation 类型【参考方案2】:在 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;
【讨论】:
恕我直言,这并不能回答他区分两个注释的问题 如果您需要检查不同的注释,那么在相同的委托方法中,您可以进行如下检查,然后相应地设置引脚颜色............ if(annotation.坐标.latitude==42.0000 && annotation.coordinate.longitude==-87.65000) 在操作的情况下,不同的注释是相同的,除了时间。所以他需要一个新的财产以上是关于MKPinAnnotationView 的不同颜色取决于时间的主要内容,如果未能解决你的问题,请参考以下文章