How to change marker icon?
Asked Answered
B

1

3

I was wonderring if there is a way to change those red pins that are used as markers. And if there is a way, how to do it?

Blagoveshchensk answered 7/12, 2012 at 11:7 Comment(3)
Do you mean the red Badges used for such things as local and push notifications?Guyton
Or do you mean the red pins shown in the map? #3193150Chandachandal
i mean red pins shown on map.Blagoveshchensk
D
6

you can use 3 types of color pins in mapView are bellow..

  1. MKPinAnnotationColorGreen;
  2. MKPinAnnotationColorPurple
  3. MKPinAnnotationColorRed

and if you want to add customview or image then you can add with programatically

also you can change pin in delegate method of MKMapView like bellow..

- (MKAnnotationView *)mapView:(MKMapView *)_mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{
 MKPinAnnotationView *pinView  = (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
        if (annotation == _mapView.userLocation) 
        {
//            pinView.pinColor = MKPinAnnotationColorRed;
//            return pinView;
            return nil;
        }
        pinView.pinColor = MKPinAnnotationColorGreen; // you can use MKPinAnnotationColorPurple, MKPinAnnotationColorRed;
        pinView.canShowCallout = YES;
        pinView.animatesDrop = NO;

        return pinView;
}

and for custom image or pin, use bellow code..

- (MKAnnotationView *)mapView:(MKMapView *)_mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{
    static NSString *AnnotationViewID = @"annotationViewID";

    MKAnnotationView *annotationView = (MKAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

    if (annotationView == nil)
    {
        annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
    }

    annotationView.image = [UIImage imageNamed:@"yourImageName"];//add any image which you want to show on map instead of red pins
    annotationView.annotation = annotation;

    return annotationView;
}
Delapaz answered 7/12, 2012 at 11:53 Comment(1)
@Blagoveshchensk see the updated answer dude.. its simple that you can add image instead of red pins on mapview.. :)Delapaz

© 2022 - 2024 — McMap. All rights reserved.