I am having a SERIOUSLY hard time trying to get MKAnnotation working . I Googled this so much today, spent a DECENT number of hours with no luck . Can someone post some code as to how this thing is supposed to be declared or how these protocols are supposed to be set up? Please give me some kind of step-by-step help. I have my MKMapView ready to go, I just can't create this MKAnnotation object for the life of me.
I want the simplest case: Add an MKPinAnnotationView at the user's current location.
I am having a SERIOUSLY hard time trying to get MKAnnotation working . I Googled this so much today, spent a DECENT number of hours with no luck . Can someone post some code as to how this thing is supposed to be declared or how these protocols are supposed to be set up? Please give me some kind of step-by-step help. I have my MKMapView ready to go, I just can't create this MKAnnotation object for the life of me.
I want the simplest case: Add an MKPinAnnotationView at the user's current location.
An annotation object is ANY object that implements the MKAnnotation protocol. The MKAnnotation protocol is painfully simple: It's just an object that has a property coordinate, and optionally properties title and subtitle.
You declare that an object supports a protocol by putting the protocol name in angle brackets at the end of the @interface declaration. If an object supports more than one protocol, you just list the protocols inside angle brackets, separated by commas. The relevant line in the MyAnnotation object is:
Code:
@interface MyAnnotation : NSObject <MKAnnotation>
Here is the header file for a sample annotation object:
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
I'm sorry about my previous post, I was looking at the Apple Reference for this protocol and it stated the coordinate as readonly, which gave me problems when assigning its @property and then synthesizing its setters and getters. However:
Basically, I have my class set up as you mention. I tried the following:
I'm sorry about my previous post, I was looking at the Apple Reference for this protocol and it stated the coordinate as readonly, which gave me problems when assigning its @property and then synthesizing its setters and getters. However:
Basically, I have my class set up as you mention. I tried the following:
I get this error: warning: class 'MKPinAnnotationView' does not implement the 'MKAnnotation' protocol
You've got the sequence wrong. You create an annotation object and add it to the map. Then, later, the system asks for an annotation view to display that annotation on the map. The code you posted should look like this:
Then, you need to add a mapView:viewForAnnotation: method to your map view delegate. That method gets called when the system needs an annotation view to display the annotation you just created.
Disclaimer: I banged out the code I'm posting without even checking to make sure it compiles, much less runs without errors. My code rarely compiles the first time, so there are likely syntax errors in the code above.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
You've got the sequence wrong. You create an annotation object and add it to the map. Then, later, the system asks for an annotation view to display that annotation on the map. The code you posted should look like this:
Then, you need to add a mapView:viewForAnnotation: method to your map view delegate. That method gets called when the system needs an annotation view to display the annotation you just created.
Disclaimer: I banged out the code I'm posting without even checking to make sure it compiles, much less runs without errors. My code rarely compiles the first time, so there are likely syntax errors in the code above.
No worries, but I do get some errors which I don't know how to deal with:
Code:
- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)anAnnotation
{
if ([anAnnotation isKindOfClass:MapAnnotation]) <- expected expression before 'MapAnnotation'
{
MapAnnotation *theAnnotation = [[MapAnnotation alloc] init];
theAnnotation = (MapAnnotation *)anAnnotation; <- 'theAnnotation' undeclared (first use in this function)
if (theAnnotation.identifier == @"StartAnnotation")
{
MKPinAnnotationView *startAnnotationPin = [[MKPinAnnotationView alloc] initWithAnnotation:anAnnotation reuseIdentifier:@"StartPin"];
startAnnotationPin.pinColor = MKPinAnnotationColorGreen;
return startAnnotationPin;
}
return nil;
}
} <-- control reaches end of non-void function
I guess I should note I included an NSString named identifier in the class of MapAnnotation...
Duncan, thanks so much for your help, it went a long way. I never did map annotations until now, and the way they are implemented in the framework is extremely weird IMHO, but anyways, thank you. Without your guidance I don't know how else I could have understood the MKAnnotation class.