开发者

MKPinAnnotationView and custom classes conforming to the MKAnnotation protocol

I have created the following method for determining the view for my annotations in a map view.

- (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id <MKAnnotation>)annotation
{

if ([annotation isKindOfClass:[MKUserLocation class]]) 
{
    return nil;
}

MKPinAnnotationView *pin;

if ([annotation isKindOfClass:[AnnotationsWithIndices class]])
{
int i = [(AnnotationsWithIndices *)annotation index];


if (i > currentCheckpointIndex ) 
{

    pin = (MKPinAnnotationView *)[mv dequeueReusableAnnotationViewWithIdentifier:@"unvisited"]; 
    if (!pin) 
    {
        pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"unvisited"];
    }
    [pin setPinColor:MKPinAnnotationColorRed]; 
}   

if (i == currentCheckpointIndex) 
{
    pin = (MKPinAnnotationView *)[mv dequeueReusableAnnotationViewWithIdentifier:@"current"];
    if (!pin) 
    {
        pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];
    }
    [pin setPinColor:MKPinAnnotationColorGreen]; 
}

if (i < currentCheckpointIndex) 
{
    pin = (MKPinAnnotationView *)[mv dequeueReusableAnnotationViewWithIdentifier:@"visited"]; 
    if (!pin) 
    {
        pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"visited"];
    }
    [pin setPinColor:MKPinAnnotationColorPurple];
}
[pin setAnimatesDrop:YES];
return [pin autorelease];
}
else
     return nil;
}

The idea is that I want the different annotation views (pins) be of different colors depending on whether the user has visited them to signal which annotation to visit next.

This code works fine, but I have a few questions about it which I hope someone can answer.

Firstly, the MKPinAnnotations are dequeued from a map view and reused whenever possible. In the line doing so (which I have found in multiple blogs and forums)

pin = (MKPinAnnotationView *)[mv dequeueReusableAnnotationViewWithIdentifier:@"unvisited"]; 

I understand that the return value of the dequeueReusableAnnotationViewWithIdentifier: is an instance of MKAnnotationView, whereas pin is a pointer to an instan开发者_StackOverflow社区ce of MKPinAnnotationView (which is a subclass o MKAnnotationView). This, I guess, is why there is a 'cast' seemingly going on with the prefix (MKPinAnnotationView *) in front of the method call. Is this really a cast and in this case, is it not dangerous since the MKPinAnnotationView contains more instance variables (than MKAnnotationView) and hence more space in memory?

I have tried to find some information about this, but nowhere I have not found anyone explaining this in particular.

Furthermore, the pointer annotation is either of class MKUserLocation or of my own custom class AnnotationsWithIndices conforming to the MKAnnotation protocol. Now, in order to determine which color the annotation view should have I have added an instance variable called index into the AnnotationsWithIndices class. Now, when I call the getter for the index I write

int i = [(AnnotationsWithIndices *)annotation index]; 

Now, I basically have the same question about this. Is there a cast going on here or is it just to let the compiler know that it is ok to send the message index to annotation? I guess that the compiler expects annotation to be an id whereas it really is a pointer to an instance of AnnotationsWithIndices. Of course I know that this is the case since the annotation will be of my custom class, and I also check this explicitly to be sure. Is the (AnnotationsWithIndices) there only to signal to the compiler that this is OK?

I have also tried to find information about this without luck.

I am very thankful for any answers on this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜