简体   繁体   English

iPhone-未将自定义MKAnnotation添加到MapView

[英]iPhone - adding custom MKAnnotation to MapView does not get added

for some reason I am having some problems with my custom MKAnnotation class and having it added to the map. 由于某种原因,我的自定义MKAnnotation类遇到了一些问题,并将其添加到地图中。 I have also attempted to use the basic MKPointAnnotation to see if this would work, however it does not. 我也尝试过使用基本的MKPointAnnotation来查看是否可行,但是不行。 In my function I create the Annotation, and then add it, while printing NSLogs to verify that the data is in the annotation. 在我的函数中,创建Annotation,然后添加它,同时打印NSLogs以验证数据是否在注释中。 When it gets to printing the contents of map's annotations array, the count is 0. Does anyone know why it is not getting added to the mapView? 当要打印地图注释数组的内容时,计数为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: 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: 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

Figured out what it was, all of the code was correct, however I was trying to modify data in the View Controller from another class, so I was doing [self.storyboard instantiate...@""] however this was creating another object and modifying that instead of modifying the original view controller. 弄清楚它是什么,所有代码都是正确的,但是我试图从另一个类修改View Controller中的数据,所以我在做[self.storyboard instantiate...@""]但是这正在创建另一个对象并对其进行修改,而不是修改原始视图控制器。 Thank you mattgalloway & Cyril! 谢谢mattgalloway&西里尔!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM