Search code examples
iosmapkitmapkitannotation

Map not showing annotation


I am including an annotation to a mapView, but it is not shown.

The map appears and it is centered at the expected location. The map center coordinates are the same as the annotation coordinates.

Any help to detect where is the problem is welcome.

#import "mapKitViewController.h"
#import "Annotation.h"

@interface mapKitViewController ()

@end

//AYUNTAMIENTO DE LAS PALMAS DE GC
#define LP_LATITUDE 28.1249625;
#define LP_LONGITUDE -15.4286931;
//span
#define THE_SPAN 0.01f;



@implementation mapKitViewController
@synthesize myMapView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Localízame";    


    //DATOS DEL MAPA
    //region
    MKCoordinateRegion myRegion;

    //centro del mapa
    CLLocationCoordinate2D center;
    center.latitude = LP_LATITUDE;
    center.longitude = LP_LONGITUDE;

    //span
    MKCoordinateSpan span;
    span.latitudeDelta = THE_SPAN;
    span.longitudeDelta = THE_SPAN;

    myRegion.center = center;
    myRegion.span = span;

    [myMapView setRegion:myRegion animated:YES];

    //annotation
    CLLocationCoordinate2D ayuntamiento;
    ayuntamiento.latitude = LP_LATITUDE;
    ayuntamiento.longitude = LP_LONGITUDE;

    Annotation *aytoPin;
    aytoPin.coordinate = ayuntamiento;
    aytoPin.title = @"Ayuntamiento";
    aytoPin.subtitle = @"Las Palmas de Gran Canaria";
    [myMapView addAnnotation:aytoPin];
}

Solution

  • I do not know what Annotation is (instead of a built-in MKPointAnnotation or similar), but I assume that it is something that knows how to draw itself correctly.

    The problem, then, is this:

    Annotation *aytoPin;
    aytoPin.coordinate = ayuntamiento;
    

    You are not creating an Annotation (alloc, init). So aytoPin is nil, and nothing is happening. There is no object here.

    (It is easy to get caught by this kind of mistake, because your subsequent messages are messages directed at nil, which is not an error in Objective-C. So nothing happens to alert you to the problem.)