【问题标题】:iPhone - adding custom MKAnnotation to MapView does not get addediPhone - 没有添加自定义 MKAnnotation 到 MapView
【发布时间】: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


【解决方案1】:

弄清楚它是什么,所有代码都是正确的,但是我试图从另一个类修改视图控制器中的数据,所以我在做[self.storyboard instantiate...@""] 但是这是创建另一个对象并修改它而不是修改原始视图控制器。谢谢 mattgalloway 和 Cyril!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    相关资源
    最近更新 更多