iPhone - добавление пользовательской MKAnnotation в MapView не добавляется - PullRequest
1 голос
/ 27 марта 2012

по некоторым причинам у меня возникли некоторые проблемы с моим пользовательским классом MKAnnotation и добавлением его на карту. Я также попытался использовать базовую аннотацию MKPoint, чтобы увидеть, будет ли это работать, однако это не так. В своей функции я создаю аннотацию, а затем добавляю ее, печатая NSLogs, чтобы убедиться, что данные находятся в аннотации. Когда дело доходит до печати содержимого массива аннотаций карты, счетчик равен 0. Кто-нибудь знает, почему он не добавляется в mapView?

- (void)viewDidLoad {
    [map setMapType:MKMapTypeStandard];
    [map setDelegate:self];
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(30.451667, -84.268533), 16090.344, 16090.344);
    viewRegion = [map regionThatFits:viewRegion];
    [map setRegion:viewRegion animated:YES];
    [map setShowsUserLocation:YES];
} 

- (void)loadBuilding {
        if (buildAnnotation != nil) {
            [map removeAnnotation:buildAnnotation];
        }
        buildAnnotation = [[BuildingAnnotation alloc] initWithCoordinate:building.getLocation withName:building.getName withAddress:building.getAddress];
        [map addAnnotation:buildAnnotation];
        [map setCenterCoordinate:buildAnnotation.coordinate animated:YES];
        NSLog(@"%@\n%@\n %f, %f", buildAnnotation.title, buildAnnotation.subtitle, buildAnnotation.coordinate.latitude, buildAnnotation.coordinate.longitude);
        NSLog(@"%i", [[map annotations] count]);
    }

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    NSLog(@"IN DELEGATE");
    static NSString *identifier = @"MyLocation";
    if ([annotation isKindOfClass:[BuildingAnnotation class]]) {

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

        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;
        annotationView.pinColor = MKPinAnnotationColorGreen;        
        return annotationView;
    }

    return nil;
}

BuildingAnnotation.h:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface BuildingAnnotation : NSObject <MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}

@property (nonatomic, copy) NSString *title; 
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

-(id)initWithCoordinate:(CLLocationCoordinate2D) c;
-(id)initWithCoordinate:(CLLocationCoordinate2D)c withName:(NSString*)name withAddress:(NSString*)address;
- (CLLocationCoordinate2D) coordinate;

@end

BuildingAnnotation.m:

#import "BuildingAnnotation.h"

@implementation BuildingAnnotation

@synthesize coordinate, title, subtitle;

- (id)initWithCoordinate:(CLLocationCoordinate2D) c {
    coordinate = c;
    title = @"Title";
    subtitle = @"Subtitle";
    return self;
}

-(id)initWithCoordinate:(CLLocationCoordinate2D)c withName:(NSString*)name withAddress:(NSString*)address {
    self = [super init];
    if (self) {
        coordinate = c;
        title = name;
        subtitle = address;
    }
    return self;
}

- (CLLocationCoordinate2D) coordinate {
    return coordinate;
}


@end

1 Ответ

0 голосов
/ 28 марта 2012

Выяснил, что это было, весь код был правильным, однако я пытался изменить данные в View Controller из другого класса, поэтому я делал [self.storyboard instantiate...@""], однако это создавало другой объект и изменяло его вместо измененияоригинальный вид контроллера.Спасибо, Mattgalloway & Кирилл!

...