2

我使用下面的循环来填充我的 MapView。但是,无论我进行多少次迭代,它总是一次只显示一个引脚。

单独声明项目似乎也没有影响。

我在 osx 10.5.8 上使用带有 xCode 3.1.3 的初始 3.0 SDK,3.1 SDK 更改日志没有提到对 MKMapKit 框架的任何修复,所以我觉得不需要下载 2.5GB文件。

    for(NSDictionary* dict in results ){
    NSLog(@"Made Annotation  %@ at N%f E%f", [dict valueForKey:@"location"],[dict valueForKey:@"latitude"],[dict valueForKey:@"longitude"] );
    NSLog(@"List of keys %@", dict);

    LTAnnotation* pin = [[LTAnnotation alloc] initWithTitle: [dict valueForKey:@"location"]
                                     latitude: [dict objectForKey:@"latitude"]
                                    longitude: [dict objectForKey:@"longitude"]
    ];

    [MapView addAnnotation: pin];

}

这是第一个日志记录语句的输出

Made Annotation  London at N51.3 E0.07000000000000001
Made Annotation  Amsterdam at N52.22 E4.53

第二个是字典的结构

List of keys {
    id = 0;
    latitude = 51.3;
    location = London;
    longitude = 0.07000000000000001;
    time = "12:00-13:00";
}
List of keys {
    id = 1;
    latitude = 52.22;
    location = Amsterdam;
    longitude = 4.53;
    time = "12:00-13:00";
}

如果您感兴趣,这里是我的 LTAnnotation 实现

@interface LTAnnotation(Private)
    double longitude;
    double latitude;
@end

@implementation LTAnnotation

@synthesize title;
@synthesize subTitle;
-(id) initWithTitle:(NSString*)pTitle latitude:(NSNumber*)latDbl longitude:(NSNumber*) longDbl{
    self = [super init];

    self.title = pTitle;

    latitude = [latDbl doubleValue];
    longitude = [longDbl doubleValue];
    NSLog(@"Create Annotation for %@ at %fN %fE",pTitle,[latDbl doubleValue],[longDbl doubleValue]);
    return self;

}


-(CLLocationCoordinate2D) coordinate
{
    CLLocationCoordinate2D retVal;

    retVal.latitude = latitude;
    retVal.longitude = longitude;

    return retVal; 
}
@end

这一切结合起来产生了这个......

替代文字 http://img340.imageshack.us/img340/3788/pi​​cture1fg.png

我哪里出错了有什么想法吗?谢谢

4

2 回答 2

1

尝试将纬度和经度设置为浮点数。

MKMapView 显示错误保存的区域

于 2009-09-30T01:15:46.920 回答
1

我注意到的两件小事可能有助于解决问题:

  • 您没有在第一个代码示例中释放引脚,这会导致泄漏
  • 您没有检查“self = [super init];” 在您的第二个代码示例中成功(“if(self = [super init]){...} return self”)。NSLog 也只输出传递给 init 方法的参数,而不是对象的实例方法

最重要的是,我刚刚在您的 init 方法中注意到了这一点:

latitude = [latDbl doubleValue];
longitude = [longDbl doubleValue];

您没有使用 Objective-C 2 风格的访问器方法(self.latitude = ...),也没有保留自动释放的值。这可能意味着变量正在消失,这就是您看不到注释的原因,因为它们没有有效的坐标。

于 2009-10-16T10:01:18.807 回答