Search code examples
objective-cannotationsmkmapviewmkannotationview

Xcode 4.3.3 Mapview, annotation not showing


My annotation (homeMark) are not showing in mapView. I have made my viewController delegate in IB

For some reason in the delegate method if ([annotation isKindOfClass:[HomeMark class]]) {

Fails.....Annotation is not part of "Homemark" class...

What am i doing wrong ?

code...

- (IBAction)clubHouse:(id)sender{
    MKCoordinateRegion region = { {0.0, 0.0 } , {0.0,0.0} };
    region.center.latitude = 55.305858;
    region.center.longitude = 12.386036;


    CLLocationCoordinate2D coord = {
        .latitude = region.center.latitude, 
        .longitude = region.span.longitudeDelta};

    HomeMark *homeMark = [[HomeMark alloc] 
                          initWithCoordinate:coord 
                          andMarkTitle:@"Klubhus" 
                          andMarkSubTitle:@"Stevns Cykel Motion"];

    [mapMyView addAnnotation:homeMark];

    [mapMyView setMapType:MKMapTypeStandard];
    [mapMyView setZoomEnabled:YES];
    [mapMyView setScrollEnabled:YES];
    region.span.longitudeDelta = 0.007f;
    region.span.latitudeDelta = 0.007f;
    [mapMyView setRegion:region animated:YES];
    //[mapMyView setDelegate:sender];    
    mapMyView.showsUserLocation = YES;

}

And the ViewForAnnotation.

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

    static NSString *identifier = @"MyLocation";
    if ([annotation isKindOfClass:[HomeMark class]]) {

        MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil) {
            annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        }
        else {
            annotationView.annotation = annotation;
        }

        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;

        return annotationView;
    }

    return nil; 
}

Solution

  • It looks like you are specifying the coordinates incorrectly so that the annotation is not where you expect.

    Instead of this:

    CLLocationCoordinate2D coord = {
        .latitude = region.center.latitude, 
        .longitude = region.span.longitudeDelta};  // <-- span doesn't make sense
    

    try this:

    CLLocationCoordinate2D coord = {
        .latitude = region.center.latitude, 
        .longitude = region.center.longitude};  // <-- not span