objective-cmkmapviewmkannotationmkpinannotationview

Different colour of MKPinAnnotationColor dependant on time


I am kind of new to coding, around 4 weeks experience so go easy on me.

I basically want to show a green pin on the map during specified times and when it is not these times a red pin will be shown.

Currently with the code for setting two different pins to appear at specified times is working, however I am having trouble implementing a different coloured pin for each instance.

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

I understand that I must use (in MapViewController.m):

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

But then get quite lost... I have tried renaming the second pin something other than myAnn and setting MKPinAnnotationColorRed or MKPinAnnotationColorGreen dependant on the name but couldn't figure it out.

If anyone could shed some light on this I would be very grateful.

Cheers guys


Solution

  • Two be able to different between the two annotations you should have a special property on the Annotation class .. e.g. a tag

          //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
    

    }

    THEN switch on the tag

    - (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;
    }