我正在编写一个iPad应用程序(os 3.2),它使用MKMapKit在地图上显示移动注释。信息通过XML从三个不同的源检索,并在我的Annotation类中进行整理,然后显示在Map上。此信息每5秒钟发送一次。我的工作正常几个月,我的注释按要求移动。
所有下面的代码都是代码的近似值(不能发布我的公司的确切代码),并且它是逐行输入的,所以它不会编译。
我的注释代码看起来像这样:
@interface MyFunkyAnnotation : NSObject <MKAnnotation>
{
CLLocationCoordinated2D coordinate;
NSString *identifier;
// Lots of other fields that can be displayed.
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *identifier;
// Lots of other properties as above.
-(void)updateFunkyAnnotationWithNewAnnotation:(MyFunkyAnnotation*)newFunky;
@end
然后在我的实现中我有类似的东西:
@implementation MyFunkyAnnotation
@synthesize coordinate;
-(void)updateFunkyAnnotationWithNewAnnotation:(MyFunkyAnnotation*)newFunky
{
[self setCoordinate:[newFunky coordinate]];
[self setIdentifier:[newFunky identifier]];
// Lots more updating of various properties.
}
@end
这很有效,所以我当然决定重新设计整个事情,并将大量信息封装到其他类中。
所以现在我的代码如下:
@interface MyFunkyAnnotation: NSObject <MKAnnotation>
{
SourceInfo_1 *source1; // Contains its own coordinate property;
SourceInfo_2 *source2; // Contains its own coordinate property;
SourceInfo_3 *source3; // Contains its own coordinate property;
}
@property (nonatomic, retain) SourceInfo_1 *source1;
@property (nonatomic, retain) SourceInfo_2 *source2;
@property (nonatomic, retain) SourceInfo_3 *source3;
-(void)updateFunkyAnnotationWithNewAnnotation:(MyFunkyAnnotation*)newFunky;
-(CLLocationCoordinate2D)coordinate;
@end
新的实施:
@implementation MyFunkyAnnotation
@synthesize source1, source2, source3;
-(CLLocationCoordinate2D)coordinate
{
if ([source1 dataReliable] == YES)
{
return [source1 coordinate];
}
else if ([source2 dataReliable] == YES)
{
return [source2 coordinate];
}
else
{
return [source3 coordinate];
}
}
-(void)updateFunkyAnnotationWithNewAnnotation:(MyFunkyAnnotation*)newFunky;
{
if ([newFunky source1] != nil)
{
[self setSource1:[newFunky source1];
}
if ([newFunky source2] != nil)
{
[self setSource2:[newFunky source2];
}
if ([newFunky source3] != nil)
{
[self setSource3:[newFunky source3];
}
}
@end;
运行此新代码会导致注释添加到原始位置,但它们根本不会移动。当我获得XML feed时,我正在更新source1,source2,source3中的坐标。
经过一整天的调试和尝试各种各样的事情,我得到了它的工作。然后我开始删除我在一天中添加的所有内容,以便进行以下最小的更改以使其正常工作并且结果非常奇怪:
界面未经修改。 在实现中,我添加了一个方法并添加了另外三行:
@implementation MyFunkyAnnotation
@synthesize source1, source2, source3;
-(void)setCoordinate:(CLLocationCoordinate2D)coordinate // <-- New method
{
nil;
}
-(CLLocationCoordinate2D)coordinate
{
if ([source1 dataReliable] == YES)
{
return [source1 coordinate];
}
else if ([source2 dataReliable] == YES)
{
return [source2 coordinate];
}
else
{
return [source3 coordinate];
}
}
-(void)updateFunkyAnnotationWithNewAnnotation:(MyFunkyAnnotation*)newFunky;
{
if ([newFunky source1] != nil)
{
[self setSource1:[newFunky source1];
[self setCoordinate:[[newFunky source1] coordinate]]; // <--- New Line
}
if ([newFunky source2] != nil)
{
[self setSource2:[newFunky source2];
[self setCoordinate:[[newFunky source2] coordinate]]; // <--- New Line
}
if ([newFunky source3] != nil)
{
[self setSource3:[newFunky source3];
[self setCoordinate:[[newFunky source3] coordinate]]; // <--- New Line
}
}
@end;
任何人都可以解释为什么需要调用一个实际上没有做任何事情的方法。 “nil”是一个NSLog语句,所以我知道它被调用了。这完全没有任何意义。
非常感谢任何见解。
干杯,
布雷特
答案 0 :(得分:2)
我的猜测是,这与未实现坐标属性有关。来自MKAnnotation Protocol文档。
采用此协议的对象必须实现坐标属性。
也许因为你没有坐标属性,你需要setCoordinate方法才能正确处理注释。
您是否尝试过添加
@synthesize coordinate;
到你的.m文件?假设你的标题中仍然有属性声明吗?
您可能必须在属性声明中专门为您的方法设置getter。像
这样的东西@property (nonatomic, assign, getter=getCoordinate) CLLocationCoordinate2D coordinate;
然后将坐标方法重命名为getCoordinate。
注意:可以避免使用getter属性并更改坐标访问者名称,但我还没有测试过。
答案 1 :(得分:0)
setCoordinate:
设置注释的新中心点。 - (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate 参数
newCoordinate
The new center point for the annotation.
讨论
支持拖动的注释应该实现此方法来更新注释的位置。 可用性
**Available in iOS 4.0 and later.**
宣布进入 MKAnnotation.h