1

我正在使用 Mapkit,并且使用的是 SDK 4.2,我在这里遇到了一个奇怪的错误,实际上我有 3 种注释类型,“blue.png”、red.png、black.png。我通过助焊剂加载这些,并根据类型选择这些注释类型。加载地图时一切正常,我有不同的注释视图,但是当我移动、放大或缩小注释视图时,注释视图会发生变化,即它应该是 blue.png 的位置变成了 black.png。

我实际上是在设备上测试它。

非常感谢你 :)

4

2 回答 2

1

嘿,问题是,如果用户平移地图以查看另一个位置,然后返回到绘制注释的位置,则会调用此方法。

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

我已经看到了许多地图应用程序的示例代码,这是大多数人正在使用的。

- (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
    if(annotationView)
        return annotationView;
    else
    {
        MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
                                         initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
        pinView.animatesDrop=YES;
        pinView.canShowCallout=YES;
        pinView.draggable = YES;
        pinView.pinColor = MKPinAnnotationColorGreen;
        return pinView;
    }
    return nil;
}
于 2011-09-07T08:55:17.543 回答
0

我找到了解决方案 - 事实上我正在使用自定义注释视图并拥有 3 种不同类型的图像:

索尔恩:

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

    AnnotationView *annotationView = nil;

    // determine the type of annotation, and produce the correct type of annotation view for it.
    AnnotationDetails* myAnnotation = (AnnotationDetails *)annotation;

    if(myAnnotation.annotationType == AnnotationTypeGeo)
    {
// annotation for your current position
        NSString* identifier = @"geo";
        AnnotationView *newAnnotationView = (AnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        if(nil == newAnnotationView)
        {
            newAnnotationView = [[[AnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:identifier] autorelease];
        }

        annotationView = newAnnotationView;
    }
    else if(myAnnotation.annotationType == AnnotationTypeMyfriends)
    {
        NSString* identifier = @"friends";
AnnotationView *newAnnotationView = (AnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        if(nil == newAnnotationView)
        {
            newAnnotationView = [[[AnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:identifier] autorelease];
        }

        annotationView = newAnnotationView;
}
}
于 2011-02-28T23:49:16.370 回答