Objective c MKPinAnnotationColor的不同颜色取决于时间
我是一个新的编码,大约4周的经验,所以请放心 我基本上想在指定的时间在地图上显示一个绿色的pin,如果不是这些时间,将显示一个红色的pin 目前,用于设置两个不同pin在指定时间出现的代码正在运行,但是我在为每个实例实现不同颜色的pin时遇到了问题 注释.hObjective c MKPinAnnotationColor的不同颜色取决于时间,objective-c,mkmapview,mkannotation,mkpinannotationview,Objective C,Mkmapview,Mkannotation,Mkpinannotationview,我是一个新的编码,大约4周的经验,所以请放心 我基本上想在指定的时间在地图上显示一个绿色的pin,如果不是这些时间,将显示一个红色的pin 目前,用于设置两个不同pin在指定时间出现的代码正在运行,但是我在为每个实例实现不同颜色的pin时遇到了问题 注释.h #import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface Annotation : NSObject <MKAnnotati
#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
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:UITableViewController{
IBUILabel*当前日期;
NSString*今天;
MKMapView*mapView;
}
@属性(强,非原子)IBMMapView*mapView;
@结束
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.h”
#导入“SWRevealViewController.h”
#进口
#进口
#导入“Annotation.h”
@接口MapViewController()
@结束
@MapViewController的实现{
}
@综合地图视图;
-(无效)viewDidLoad{
self.mapView.delegate=self;
NSLocale*locale=[[NSLocale alloc]initWithLocaleIdentifier:@“en_GB”];
NSDateFormatter*格式化程序=[[NSDateFormatter alloc]init];
[格式化程序setLocale:locale];
[格式化程序setTimeStyle:NSDateFormatterShortStyle];
[格式化程序setDateFormat:@“EEEE HH:mm”];
todaysDate=[formatter stringFromDate:[NSDate date]];
currentDate.text=今天;
[超级视图下载];
//注释
NSMUTABLEARRY*位置=[[NSMUTABLEARRY alloc]init];
CLLOCATION坐标2D定位;
注释*myAnn;
//区域
//地点
如果(
//周一
[今天包含字符串:@“星期一17”]| |[今天包含字符串:@“星期一18”]||
//周二
[今天包含字符串:@“星期二17”]| |[今天包含字符串:@“星期二18”]||
//结婚
[今天包含字符串:@“星期三17”]| |[今天包含字符串:@“星期三18”]||
//星期四
[今天包含字符串:@“星期四17”]| |[今天包含字符串:@“星期四18”]||
//星期五
[今天包含字符串:@“星期五17”]| |[今天包含字符串:@“星期五18”]||
//坐
[今天包含字符串:@“星期六17”]| |[今天包含字符串:@“星期六18”]||
//太阳
[todaysDate包含字符串:@“Sunday 17”]| |[todaysDate包含字符串:@“Sunday 18”]
)
{
//绿色的
myAnn=[[Annotation alloc]init];
位置纬度=51.4;
位置经度=-0.1;
myAnn.坐标=位置;
myAnn.title=@“场馆1”;
myAnn.subtitle=@“subtitle 1”;
[对象:myAnn];
[self.mapView addAnnotations:位置];
//绿色的
}否则{
//红色的
myAnn=[[Annotation alloc]init];
位置纬度=51.4;
位置经度=-0.1;
myAnn.坐标=位置;
myAnn.title=@“场馆1”;
myAnn.subtitle=@“subtitle 1”;
[对象:myAnn];
[self.mapView addAnnotations:位置];
//红色的
}
@结束
我理解我必须使用(在MapViewController.m中):
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
但我还是迷路了……我尝试过将第二个pin重命名为myAnn以外的名称,并根据名称设置MKPinAnnotationColorRed或MKPinAnnotationColorGreen,但无法理解
如果有人能解释这一点,我将不胜感激
干杯伙计们在MKMapView代理“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;
如果两个注释能够在两个注释之间有所不同,则注释类上应该有一个特殊属性。例如,标记
//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;
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
//只处理我们的注释
如果(![annotation isKindOfClass:[annotation class]])
返回;
MKPinAnnotationView*pinView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“CustomPinAnnotationView”];
pinView.animatesDrop=是;
pinView.canShowCallout=是;
pinView.image=[UIImage ImageName:@“pizza_slice_32.png”];
pinView.pinColor=MKPinAnnotationColorPurple;
如果((Annotation.tag==0)){
pinView.pinColor=MKPinAnnotationColorGreen;
}
否则{
pinView.pinColor=MKPinAnnotationColorRed;
}
回位销;
}
请检查。这并不能回答他关于区分两个注释的问题。如果您需要检查不同的注释,那么在相同的委托方法中,您可以进行如下检查,然后相应地设置pin颜色………如果(annotation.coordinate.latitude==42.0000&&annotation.coordinate.longitude==-87.65000)在op的例子中,不同的注释除了时间之外是相同的。所以他需要一个新的属性Tanks Daji,这正是我想要的。我得到了“在类型为“Annotation”的对象上找不到属性“tag”。我假设我必须在批注中设置一些内容。h并在批注中合成它。m.@Property(nonatomic,assign)X*标记;类似于此的内容?不确定X值应该是什么。好的,我已经设置了@Property(nonatomic,assign)NSInteger标记;但我仍然得到错误“在类型为“Annotation”的对象上找不到属性“tag”,有什么想法吗?哦,糟糕。((Annotation*)Annotation.tag::typo;)必须将变量强制转换为批注类型
//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;
}