Apps
Apps

Reputation: 103

How do I change the pin color of an annotation in map kit view

I would like to know how to change the pin color of an annotation in map kit. Currently Im using the following code.

    //King Solomon's Lodge No. 194
CLLocationCoordinate2D kingsolomons;
kingsolomons.latitude = 38.052041;
kingsolomons.longitude = -78.683218;
Annotation *kingsolomonslodge = [[Annotation alloc] init];
kingsolomonslodge.coordinate = kingsolomons;
kingsolomonslodge.title = @"King Solomon's Lodge No. 194";
kingsolomonslodge.subtitle = @"Charlottesville, VA";
[self.myMapView addAnnotation:kingsolomonslodge];

I have googled this question a few times but Im unsure on how to implement this into the current code that I have.

Upvotes: 2

Views: 8581

Answers (5)

Vlad Grigorov
Vlad Grigorov

Reputation: 11376

Since iOS 9:

kingsolomonslodge.pinTintColor = UIColor.greenColor;

Upvotes: 0

pallavi
pallavi

Reputation: 513

If you want to apply custom color you can add image also.

 MKAnnotationView *pinView = nil; 
pinView.image = [UIImage imageNamed:@"pinks.jpg"];

Upvotes: 0

user1882026
user1882026

Reputation:

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


   if (annotation == mapview.userLocation)
    return nil;

    MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapview dequeueReusableAnnotationViewWithIdentifier: @"asdf"];

   if (pin == nil)
      pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: @"asdf"] autorelease];
   else
      pin.annotation = annotation;

 //  NSLog(@"%@",annotation.title);

    NSString *titlename=@"xyz";
   if ([annotation.title isEqualToString:titlename]) {
       pin.pinColor = MKPinAnnotationColorGreen;
     // pin.image=[UIImage imageNamed:@"arrest.png"] ;
}
else{
    pin.pinColor= MKPinAnnotationColorPurple;
}

    pin.userInteractionEnabled = YES;
    UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeCustom];
//  //pin.image=[UIImage imageNamed:@"arrest.png"] ;



pin.rightCalloutAccessoryView = disclosureButton;
//pin.pinColor = MKPinAnnotationColorRed;
pin.animatesDrop = YES;
[pin setEnabled:YES];
[pin setCanShowCallout:YES];
return pin;


}

Upvotes: 3

Midhun MP
Midhun MP

Reputation: 107221

You can use pinColor property if MKAnnotation class.

kingsolomonslodge.pinColor = MKPinAnnotationColorGreen;

Another way is you can use image instead of the default pin using viewForAnnotation: delegate:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *identifier = @"MyLocation";
    if ([annotation isKindOfClass:[yourAnnotationLocation class]])
    {

        MKAnnotationView *annotationView = (MKAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil)
        {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            annotationView.enabled = YES;
            annotationView.canShowCallout = YES;
            annotationView.image = [UIImage imageNamed:@"yourImage.png"];//here we use a nice image instead of the default pins
            annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        }
        else
        {
            annotationView.annotation = annotation;
        }
        return annotationView;
    }

return nil;
}

Upvotes: 2

NiravPatel
NiravPatel

Reputation: 3260

try this man...

kingsolomonslodge.pinColor = AnnotationColorRed;

let me know whether it is workihg or not

happy coding!!!!

Upvotes: 1

Related Questions