出于某种原因,我的自定义MKAnnotation类遇到了一些问题并将其添加到地图中。我还尝试使用基本的MKPointAnnotation来查看这是否有效,但事实并非如此。在我的函数中,我创建了Annotation,然后添加它,同时打印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
答案 0 :(得分:0)
弄清楚它是什么,所有代码都是正确的,但是我试图从另一个类修改View Controller中的数据,所以我正在做[self.storyboard instantiate...@""]
但是这是创建另一个对象并修改它修改原始视图控制器。谢谢mattgalloway&amp;西里尔!