1
votes

I found a short guide on how to load "annotation data" from a plist and add those annotations (pins) to a MapView.

My question is if anyone can tell me how I would customize the pins the most easily? As an example, I would like to define a color and an image in the plist and then have it set when adding the pins.

The pins are added like this:

MyAnnotation.m:

@implementation MyAnnotation
@synthesize title, subtitle, coordinate;

-(void) dealloc {
    [super dealloc];
    self.title = nil;
    self.subtitle = nil;
}

MyAnnotation.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MyAnnotation : NSObject<MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString* title;
@property (nonatomic, copy) NSString* subtitle;

Running through the plist in ViewDidLoad in MapViewController.m (array containg dictionaries for each pin) and adding annotations.

NSString *path = [[NSBundle mainBundle] pathForResource:@"Annotations" ofType:@"plist"];
    NSMutableArray* anns = [[NSMutableArray alloc] initWithContentsOfFile:path];

    for(int i = 0; i < [anns count]; i++) {
        float realLatitude = [[[anns objectAtIndex:i] objectForKey:@"latitude"] floatValue];
        float realLongitude = [[[anns objectAtIndex:i] objectForKey:@"longitude"] floatValue];

        MyAnnotation* myAnnotation = [[MyAnnotation alloc] init];
        CLLocationCoordinate2D theCoordinate;
        theCoordinate.latitude = realLatitude;
        theCoordinate.longitude = realLongitude;
        myAnnotation.coordinate = theCoordinate;
        myAnnotation.title = [[anns objectAtIndex:i] objectForKey:@"title"];
        myAnnotation.subtitle = [[anns objectAtIndex:i] objectForKey:@"subtitle"];
        [mapView addAnnotation:myAnnotation];
        [annotations addObject:myAnnotation];
        [myAnnotation release];
    }

EDIT:

I have added the viewForAnnotation method which sets the color but how can I set the color of each pin individually?

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
    MKPinAnnotationView *pinView = nil;

    if(annotation != mapView.userLocation) {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        pinView.pinColor = MKPinAnnotationColorPurple; 
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
    } 
    else {
        [mapView.userLocation setTitle:@"I am here"];
    }

    return pinView;
}
1
I have added the viewForAnnotation method which sets the color but how can I set the color of each pin individually?simonbs

1 Answers

0
votes

Solved! It can be done like this in the viewForAnnotation method:

for(MyAnnotation* a in mapView.annotations) {
            if(annotation == a && annotation != mapView.userLocation) {
                pinView.image = [UIImage imageNamed:a.annImage];
            }
        }