How to add more than one color pin to a mapview.

ios, iphone, objective-c, xcode

Solution

Simple enough to do, just add an if statement to check the title or subtitle. When I was using maps, my annotation class had a subtitle where it would say what the place was, i.e. hotel, airport etc. I would just add an if statement to check, and then set what the pin point was and set up what colour I wanted the pin to be

if([annotation.subtitle isEqualToString:@"Hotel"])
    {
        pinView.animatesDrop=YES;
        pinView.pinColor=MKPinAnnotationColorPurple;


    }else if([annotation.subtitle isEqualToString:@"Airport"])
    {
      pinView.animatesDrop=YES;
        pinView.pinColor=MKPinAnnotationColorRed;
    }

or if you want it to be an image, just use

pinView.image = [UIImage imageNamed:@"airportMarker.png"];

EDIT: UPDATED CODE

Create a custom annotation class like this

@interface MyAnnotation : NSObject<MKAnnotation> 
{

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

@end

Then when you are adding the objects to screen, give each one an ID, i.e

CLLocationCoordinate2D hotelCoordinates1;
hotelCoordinates1.latitude = 35.38355;
hotelCoordinates1.longitude = 1.11004;
MyAnnotation* exampleHotel=[[MyAnnotation alloc] init];
exampleHotel.coordinate=hotelCoordinates1;
exampleHotel.title=@"Example Hotel";
exampleHotel.subtitle=@"Hotel";
exampleHotel.categoryID = 1;

Then inside this method

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

just do this

if([(MyAnnotation*)annotation categoryID] == 1)
        {

        }

You need to ensure you cast the annotation to whatever the name of the class you created. And thats it. Hope that helps!!

Problem

I have a map that has 3 categories of points of interest. At the moment I have got them to display however they are all the same color. How can I get them to display a color depending to what category they are in? I have had a search around and couldn't find anything that I could understand to apply to my code. Here is my current code for annotating the pins: ``` -(void)pins:(NSString *)name lat:(NSString *)lat lon:(NSString *)lon poiID:(NSString *)poiID { //cast string to float CGFloat Lat = (CGFloat)[lat floatValue]; CGFloat Lon = (CGFloat)[lon floatValue]; //Set coordinates of pin CLLocationCoordinate2D coordinate; coordinate.latitude = Lat; coordinate.longitude = Lon; //Create Pin MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init]; //set details [annotation setCoordinate:coordinate]; [annotation setTitle:name]; [annotation setAccessibilityLabel:poiID]; //set the label eq to the id so we know which poi page to go to // Add pin to map [self.showMapView addAnnotation:annotation]; } - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { //create annotation MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pinView"]; if (!pinView) { pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"]; pinView.pinColor = MKPinAnnotationColorRed; pinView.animatesDrop = FALSE; pinView.canShowCallout = YES; //details button UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; pinView.rightCalloutAccessoryView = rightButton; } else { pinView.annotation = annotation; } return pinView; } ``` Here is the rest of my Code incase it is needed. Thanks for any help.

Original source