Mert Karabulut
Mert Karabulut

Reputation: 63

Custom Pin Image for MapKit Annotation

I want to use my Pin (not default).

I tried above code. First, I can see default pin. When I touched showMyLocation, after I touched sightLocation, default pin change with my custom pin. I want to show my custom pin in first view.

TOCGSightAnnotation.h

@interface TOCGSightAnnotation : NSObject <MKAnnotation>
@property (strong, nonatomic) NSString *title;
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title;
@end

TOCGSightAnnotation.m

@implementation TOCGSightAnnotation

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title
{
    if ((self = [super init])) {
        self.coordinate =coordinate;
        self.title = title;
    }
    return self;
}

@end

TOCGSightseeingMapKitViewController.h

@interface TOCGSightseeingMapKitViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>

@property (strong) NSNumber *latitude;
@property (strong) NSNumber *longitude;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (strong, nonatomic) CLLocationManager * locationManager;

@end

TOCGSightseeingMapKitViewController.m

#import "TOCGSightseeingMapKitViewController.h"
#import "TOCGSightAnnotation.h"

CLLocationCoordinate2D sightCoordinate;
MKCoordinateRegion region;

@interface TOCGSightseeingMapKitViewController ()

@end

@implementation TOCGSightseeingMapKitViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    [self.locationManager startUpdatingLocation];

    sightCoordinate = CLLocationCoordinate2DMake([self.latitude doubleValue], [self.longitude doubleValue]);

    TOCGSightAnnotation *annotation = [[TOCGSightAnnotation alloc] initWithCoordinate:sightCoordinate title:@"Sight Title"];
    [self.mapView addAnnotation:annotation];

    region =
    MKCoordinateRegionMakeWithDistance(sightCoordinate, 500, 500);

    [self.mapView setRegion:region animated:YES];
}

- (IBAction)myLocation:(id)sender
{
    self.mapView.delegate = self;
    self.mapView.showsUserLocation = YES;
    [self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}

- (IBAction)sightLocation:(id)sender
{
    [self.mapView setRegion:region animated:YES];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView
            viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    if ([annotation isKindOfClass:[TOCGSightAnnotation class]])
    {
        MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView
                                                                 dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];

        if (!pinView)
        {
            pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                      reuseIdentifier:@"CustomPinAnnotationView"];
            pinView.image = [UIImage imageNamed:@"mapIcon.png"];
            pinView.pinColor = MKPinAnnotationColorRed;
            pinView.animatesDrop = YES;
            pinView.canShowCallout = YES;

        }
        else
            pinView.annotation = annotation;

        return pinView;
    }

    return nil;
}

Upvotes: 0

Views: 536

Answers (2)

Mert Karabulut
Mert Karabulut

Reputation: 63

I tried this and it works.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    [self.locationManager startUpdatingLocation];
}

- (void)viewDidAppear:(BOOL)animated
{
    self.mapView.delegate = self;

    sightCoordinate = CLLocationCoordinate2DMake([self.latitude doubleValue], [self.longitude doubleValue]);

    TOCGSightAnnotation *annotation = [[TOCGSightAnnotation alloc] initWithCoordinate:sightCoordinate title:@"Sight Title"];
    [self.mapView addAnnotation:annotation];

    region =
    MKCoordinateRegionMakeWithDistance(sightCoordinate, 500, 500);

    [self.mapView setRegion:region animated:YES];
}

Upvotes: 0

Emilie
Emilie

Reputation: 2413

The reason is simple. The method

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

will get called only if you set the MKMapViewDelegate. As long as you didn't touch the button that calls the IBAction

- (IBAction)myLocation:(id)sender

the delegate is not set, so the delegate methods are not called. Try moving the line

self.mapView.delegate = self;

after your MKMapView allocation.

Upvotes: 1

Related Questions