iOs why must I implement setCoordinate to have my Annotation move in MKMapKit?
I am writing an iPad application (os 3.2) which uses MKMapKit to display moving annotations on the map. Information is retrieved via XML from three different sources and collated together in my Annotation class and then displayed on the Map. This information is coming in every 5 seconds. I had this working fine for a few months with my annotations moving as required.
All the following code is a close approximation of the code (can't post my companies exact code), and it was typed in line by line so it won't compile.
My annotation code looked something like this:
@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
Then in my implementation i had stuff like:
@implementation MyFunkyAnnotation
@synthesize coordinate;
-(void)updateFunkyAnnotationWithNewAnnotation:(MyFunkyAnnotation*)newFunky
{
[self setCoordinate:[newFunky coordinate]];
[self setIdentifier:[newFunky identifier]];
// Lots more updating of various properties.
}
@end
This worked great, so I of course decided to redesign the whole thing and encapsulate alot of the information into other classes.
So now my code looks as follows:
@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
The new implementation:
@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;
Running this new code resulted in the annotations being added in the original location but they never moved at all. I am updating the coordinates inside the source1, source2, source3 when I get the XML feeds.
So after a whole day of debugging and trying various things I got it to work. I then started to remove everything I had added in during the course of the day to get the following minimal changes to make it work and the results are quite strange:
The interface is unmodified. In the implementation I added one method and added three other lines:
@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;
Can anyone explain why the need to call a method which doesn't actually do anything. The "nil" was an NSLog statement so I know that it was being called. This makes absolutely no sense at开发者_开发技巧 all.
Any insight would be greatly appreciated.
Cheers,
Brett
My guess is that this has something to do with not implementing the coordinate property. From the MKAnnotation Protocol documentation.
An object that adopts this protocol must implement the coordinate property.
Perhaps since you don't have the coordinate property, you need the setCoordinate method in order for it work correctly handle the annotation.
Have you tried adding
@synthesize coordinate;
to your your .m file? Assuming that you still have the property declaration in your header?
You might have to specifically set the getter to your method in the property declaration. Something like
@property (nonatomic, assign, getter=getCoordinate) CLLocationCoordinate2D coordinate;
and then rename your coordinate method to getCoordinate.
NOTE: it may be possible to avoid using the getter attribute and changing the coordinate accessor name but I haven't tested that out.
setCoordinate:
Sets the new center point of the annotation. - (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate Parameters
newCoordinate
The new center point for the annotation.
Discussion
Annotations that support dragging should implement this method to update the position of the annotation. Availability
**Available in iOS 4.0 and later.**
Declared In MKAnnotation.h
精彩评论