Question

I know this question has been asked by others before and i have read them in this forum previously, but I have tried all the proposed approach without luck so far so I decided to post this question.

I have this piece of code below which is meant to change the pin icon on MKMapView. However the viewForAnnotation function doesn't even seem to get called by the MKMapView. People said that they got some problems with delegating the function to the file's owner which can be done by dragging the map view in .xib file to the file owner or defining myMap.delegate = self. I have done both ways but still get nothing.

Really appreciate for any help for my problem, thanks.

CODE:

- (MKPinAnnotationView*)myMap:(MKMapView*)myMap viewForAnnotation:(id<MKAnnotation>)annotation{

    MKPinAnnotationView *pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"CustomPin"];

    UIImage *icon = [UIImage imageNamed:@"bustour.png"];
    UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(8,0,32,37)];

    if(icon == nil)
        NSLog(@"image: ");
    else
        NSLog(@"image: %@", (NSString*)icon.description);

    [iconView setImage:icon];
    [pin addSubview:iconView];
    pin.canShowCallout = YES;
    pin.pinColor = MKPinAnnotationColorPurple;

    return pin;
}

DELEGATION enter image description here

Was it helpful?

Solution

Your delegate method is named incorrectly as myMap:viewForAnnotation:.

The viewForAnnotation delegate method must be named mapView:viewForAnnotation: like this:

- (MKAnnotationView *)mapView:(MKMapView *)mapView 
            viewForAnnotation:(id < MKAnnotation >)annotation
{
    //code goes here...
}

The map view will look for that exact method name.
If not found, it won't call it and will create a default red pin instead.


You can change the name of the internal parameters but not the method name.

For example, this would also be ok:

- (MKAnnotationView *)mapView:(MKMapView *)myMap 
            viewForAnnotation:(id < MKAnnotation >)annotation
{
    //code goes here...
}

OTHER TIPS

import bellow class in your .h class..

#import <MapKit/MapKit.h>

and also add MKMapViewDelegate in your this class like bellow...

@interface PTAViewController : UIViewController<MKMapViewDelegate>{

  ///...... your code..

}

@end

As I understand, you setting outlet, not delegate. To set delegate do like that:enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top