我对编码很陌生,大约有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,但无法弄明白。
如果有人能对此有所了解,我将非常感激。
干杯球员
答案 0 :(得分:0)
两个注释之间有两个不同,你应该在Annotation类上有一个特殊的属性。标签
//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;
}
答案 1 :(得分:-1)
在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;