【发布时间】:2012-03-27 05:24:19
【问题描述】:
由于某种原因,我的自定义 MKAnnotation 类存在一些问题,并将其添加到地图中。我还尝试使用基本的 MKPointAnnotation 来查看这是否可行,但它没有。在我的函数中,我创建注释,然后添加它,同时打印 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
【问题讨论】:
-
有点明显的问题,但是当你调用 loadBuilding 时地图是否存在?此外,NSLog 很好,但 gdb 可能会帮助您更深入地挖掘对象结构。
-
是的,地图存在,我有一个显示用户位置的地图,并且确实调用了“IN DELEGATE”的 NSLog。很抱歉,但在我 2 年的 iOS 编程中,我从未真正使用过 GDB 进行调试哈哈,你能给我一个可能有帮助的例子吗?
-
@Muller - 你真的应该学习如何使用 GDB(或者现在就学习 LLDB!)。这里有一些关于它的内容 - raywenderlich.com/10505/my-app-crashed-now-what-part-2 - 但是,为此,它很容易。只需在
loadBuilding的开头设置一个断点并检查是否设置了map(将鼠标悬停在它上面,看看它是否不是0x0)。
标签: iphone ios5 annotations mkmapview mkannotation