0

I need to change user's current location Blue Dot annotation with some custom image. How to achieve it?

here my sample code :

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

MKPinAnnotationView *pinView = nil;

    if (annotation == mapView.userLocation){

        static NSString* AnnotationIdentifier = @"user";
        MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
        MKPinAnnotationView *customPinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
        customPinView.image = [UIImage imageNamed:@"user.png"];


    customPinView.animatesDrop = YES;
    customPinView.canShowCallout = YES;
    return customPinView;

    }

But when I launch the application its showing Red marker.

Pooja Gupta
  • 785
  • 10
  • 22
batMan007
  • 551
  • 1
  • 10
  • 24

1 Answers1

0

Put this code in viewForAnnotation: method

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

static NSString* AnnotationIdentifier = @"Annotation";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];

 if (!pinView) {

    MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];   
    if (annotation == mapView.userLocation){
       customPinView.image = [UIImage imageNamed:@"myPin.png"];
    }
    else{
        customPinView.image = [UIImage imageNamed:@"myPin1.png"];
    }
    
    customPinView.canShowCallout = YES;
    customPinView.animatesDrop = NO;
    return customPinView;

} else {

    pinView.annotation = annotation;
}

 return pinView;
}
Fabio
  • 5,432
  • 4
  • 22
  • 24