MKMapKit を使用してマップ上に移動する注釈を表示する iPad アプリケーション (os 3.2) を作成しています。情報は 3 つの異なるソースから XML 経由で取得され、Annotation クラスでまとめて照合され、マップに表示されます。この情報は 5 秒ごとに送信されます。必要に応じて注釈を移動しながら、これを数か月間正常に機能させました。
次のコードはすべて、コードの近似値であり (私の会社の正確なコードを投稿することはできません)、1 行ずつ入力されているため、コンパイルされません。
私の注釈コードは次のようになります。
@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 フィードを取得するときに、source1、source2、source3 内の座標を更新しています。
そのため、丸一日デバッグしてさまざまなことを試した後、動作するようになりました。次に、その日の間に追加したすべてのものを削除して、次の最小限の変更を加えて動作させましたが、結果は非常に奇妙です:
インターフェイスは変更されていません。実装では、1 つのメソッドを追加し、さらに 3 つの行を追加しました。
@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 ステートメントだったので、それが呼び出されていたことがわかります。これはまったく意味がありません。
どんな洞察も大歓迎です。
乾杯、
ブレット