Displaying custom multiple pins shows wrong pins for locations

自作多情 提交于 2019-12-02 05:31:36

It sounds like an annotation view re-use issue.

When the annotations are re-displayed, they are re-using views with the images of previous annotations. The image property in the view is not being updated as it should be when it is re-used for another annotation.

In the viewForAnnotation delegate method, this code looks wrong:

MKAnnotationView *annotationView = [mapView dequeue...
if (annotationView == nil)
    annotationView = myLocation.annotationView;
else
    annotationView.annotation = annotation;

If the dequeue returns a view (ie. a previously-created view that may have been created for an annotation of a different type), its annotation property is updated but its image property is not updated.

The existing code only sets the image property when creating a new annotation view (when dequeue returns nil).

Right now, the annotation view creation and image-setting code is in the annotation model class IGAMapAnnotation. It would be better to create a custom MKAnnotationView class that automatically updates the image property whenever its annotation property is updated.

However, another alternative is to put all the logic in the viewForAnnotation delegate method itself (and remove the annotationView method from the IGAMapAnnotation class).

Example of the updated viewForAnnotation delegate method:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if (! [annotation isKindOfClass:[IGAMapAnnotation class]])
    {
        //return default view if annotation is NOT of type IGAMapAnnotation...
        return nil;
    }


    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"IGAMapAnnotation"];

    if (annotationView == nil)
    {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"IGAMapAnnotation"];
        //these properties don't change per annotation 
        //so they can be set only when creating a new view...
        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    }
    else
    {
        annotationView.annotation = annotation;
    }


    //whether we are using a completely new view or a re-used view,
    //set the view's image based on the current annotation...

    IGAMapAnnotation *myLocation = (IGAMapAnnotation *) annotation;
    if ([myLocation.type isEqual: @"A"] || [myLocation.type isEqual: @"B"] || [myLocation.type isEqual: @"C"])
    {
        annotationView.image = [UIImage imageNamed:@"circle.png"];
    }
    else if ([myLocation.type isEqual: @"D"])
    {
        annotationView.image = [UIImage imageNamed:@"triangle.png"];
    }
    else if ([myLocation.type isEqual: @"E"])
    {
        annotationView.image = [UIImage imageNamed:@"square.png"];
    }
    else
    {
        annotationView.image = [UIImage imageNamed:@"oval.png"];
    }


    return annotationView;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!