title: Setting Map Pin colour dynamically for iOS
body: <p>I parse an xml that contains the string 0 ,1 and 2.</p>
  
  <p>//For reference
  0 = Green
  1 = Red
  2 = Purple</p>
  
  <p>I have a class that confirms to the MKAnnotaion that contains the below variables that are properties.</p>
  
  <pre><code>CLLocationCoordinate2D  coordinate;
  NSString            *title;
  NSString            *subtitle;
  MKPinAnnotationColor pinColor;
  </code></pre>
  
  <p>This class is named MyAnnotation</p>
  
  <p>Now in the viewDidLoad of the map view I run a for loop to iterate over the parsed data
  like the below (locationArray holds this data and I pull out all the info just fine.</p>
  
  <pre><code> for (int i = 0; i &lt; locationArray.count; i++) {
      myAnnotation =[[MyAnnotation alloc] init];
  
      NSString *thePointName = [[locationArray objectAtIndex:i]xmlName];
      NSString *theAddress = [[locationArray objectAtIndex:i]xmlAddress];
  
      NSString *latString = [[locationArray objectAtIndex:i]xmlLat];
      NSString *lonString = [[locationArray objectAtIndex:i]xmlLon];
  </code></pre>
  
  <p>//This is the string that pulls out the mentioned 0, 1 or 2 strings which represent the colour of the pins poinType is retained as a string</p>
  
  <pre><code>    pointType = [[locationArray objectAtIndex:i]xmlType];
  
      double theLatitude = [latString doubleValue];
      double theLongtitude = [lonString doubleValue];
  
      userLocation.latitude=theLatitude;
      userLocation.longitude=theLongtitude;
  
      myAnnotation.coordinate=userLocation;
      myAnnotation.title=[NSString stringWithFormat:@"%@", thePointName];
      myAnnotation.subtitle=[NSString stringWithFormat:@"%@", theAddress];
  
      //I log that the points are actually giving either of the colors and if so set MyAnnotation class to the pincolor 
  
      NSLog(@"Points Color %@", pointType);
      if ([pointType isEqualToString:@"0"]){
          myAnnotation.pinColor = MKPinAnnotationColorGreen;
      }else if ([pointType isEqualToString:@"1"]){
          myAnnotation.pinColor = MKPinAnnotationColorRed;
      }else if ([pointType isEqualToString:@"2"]) {
          myAnnotation.pinColor = MKPinAnnotationColorPurple;
      }
  
      [mapView addAnnotation:myAnnotation];
  
      }
  </code></pre>
  
  <p>Now in the MKAnnotationView view i do the below</p>
  
  <pre><code>  - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id     &lt;MKAnnotation&gt;)annotation
  
   {
  
  // if it's the user location, just return nil.
      if ([annotation isKindOfClass:[MKUserLocation class]])
      return nil;
  
  // try to dequeue an existing pin view first
  static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
  MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc]
                                    initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
  pinView.animatesDrop=YES;
  pinView.canShowCallout=YES;
  
      //set pin color to the correct colour
      if (myAnnotation.pinColor = MKPinAnnotationColorGreen) {
      pinView.pinColor = MKPinAnnotationColorGreen;
      }
  
      else if (myAnnotation.pinColor = MKPinAnnotationColorRed) {
      pinView.pinColor = MKPinAnnotationColorRed;
  
       }
  
      else if (myAnnotation.pinColor = MKPinAnnotationColorPurple){
      pinView.pinColor = MKPinAnnotationColorPurple;
      }
  
  
  
      UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
  [rightButton setTitle:annotation.title forState:UIControlStateNormal];
  [rightButton addTarget:self
                  action:@selector(showDetails:)
        forControlEvents:UIControlEventTouchUpInside];
  pinView.rightCalloutAccessoryView = rightButton;
  
  
      UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage  imageNamed:@"Profile.png"]];
  pinView.leftCalloutAccessoryView = profileIconView;
  
  return pinView;
    }
  </code></pre>
  
  <p>I have also tried in the above method however it is not setting the pin colours. Everything else is fine!</p>
  
  <pre><code>    //set pin color to the correct colour
      if (pointType isEqualToString:@"0") {
      pinView.pinColor = MKPinAnnotationColorGreen;
      }
  
      else if (pointType isEqualToString:@"1") {
      pinView.pinColor = MKPinAnnotationColorRed;
  
      }
  
      else if (pointType isEqualToString:@"2"){
      pinView.pinColor = MKPinAnnotationColorPurple;
      }
  </code></pre>