Quote:
Originally Posted by Ovidius
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:
Code:
MapAnnotation *startAnnotation = [[MapAnnotation alloc] initWithCoordinate:userLocation.coordinate];
MKPinAnnotationView *startAnnotationPin = [[MKPinAnnotationView alloc] initWithAnnotation:startAnnotation reuseIdentifier:@"StartPin"];
startAnnotationPin.pinColor = MKPinAnnotationColorGreen;
[mapView addAnnotation:startAnnotationPin];
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:
Code:
MapAnnotation *startAnnotation = [[MapAnnotation alloc]
initWithCoordinate:userLocation.coordinate];
[mapView addAnnotation: startAnnotation];
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.
Something like this:
Code:
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>)annotation
{
MapAnnotation* theAnnotation = (MapAnnotation*) annotation;
if(annotation != mapView.userLocation)
{
if ([annotation isKindOfClass: [MapAnnotation class]])
{
MKPinAnnotationView *startAnnotationPin = [[MKPinAnnotationView alloc] initWithAnnotation:startAnnotation reuseIdentifier:@"StartPin"];
startAnnotationPin.pinColor = MKPinAnnotationColorGreen;
return startAnnotationPin.pinColor;
}
}
else
return nil;
}
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.