0

我在 IOS 中编写了一个程序,我在地图上用大头针标记了几个点。现在我有了 dequereusableannotationview 代码,它可以在一个引脚上正常工作,但在我使用两个引脚或尝试在地图上标记两个点时崩溃。它仅在我注释掉一个注释或注释掉 dequeucode 时才有效但问题是....我什至需要那个 dequeu 代码吗?因为我将拥有的最多的引脚可能是...10 ..?

谢谢

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];

    MKMapView *mv=[[MKMapView alloc]initWithFrame:CGRectZero];
    mv.delegate=self;
    [self setView:mv];

    MapAnnotation* ann=[[MapAnnotation alloc]init];
    CLLocationCoordinate2D location;
    location.latitude=(double)51.501468;
    location.longitude=(double)-0.141596;
    [ann setCoordinate:location];
    [ann setTitle:@"test"];
    [ann setTitle:@"plz work"];
    [mv addAnnotation:ann];
    [ann release];

    MapAnnotation* AnnB=[[MapAnnotation alloc]init];
    CLLocationCoordinate2D locationB;
    locationB.latitude=(double)16.4944;
    locationB.longitude=(double)-151.7364;
    [AnnB setCoordinate:locationB];
    [AnnB setTitle:@"test"];
    [AnnB setSubtitle:@"work"];

    [mv addAnnotation:AnnB];
    [AnnB release];
}

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    MKPinAnnotationView *pinView=nil;

    if (annotation!=mapView.userLocation) {
        static NSString *defaultID=@"MYLoction";

        pinView=(MKPinAnnotationView *)[mapView
                                        dequeueReusableAnnotationViewWithIdentifier:defaultID];
        if (pinView=nil) {
            pinView=[[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:defaultID]autorelease];

            pinView.pinColor=MKPinAnnotationColorPurple;
            pinView.canShowCallout=YES;
            pinView.animatesDrop=YES;
        }
    }
}
4

2 回答 2

0

一个问题是,当您在

if (pinView=nil) {

您正在使用赋值运算符 ,=而不是相等运算符,==。这将导致每次都nil被分配,因此您永远不会重用注释。pinView我很惊讶编译器没有捕捉到这一点。

此外,您实际上并没有在viewForAnnotation:. 试试这个:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:
    (id<MKAnnotation>)annotation {
    if(annotation == mapView.userLocation) {
        return nil;
    }

    static NSString *defaultID=@"MYLoction";

    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView 
        dequeueReusableAnnotationViewWithIdentifier:defaultID];

    if (!pinView) {
        pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation 
            reuseIdentifier:defaultID] autorelease];

        pinView.pinColor=MKPinAnnotationColorPurple;
        pinView.canShowCallout=YES;
        pinView.animatesDrop=YES;
    }

    return pinView;
}
于 2012-07-23T16:37:26.313 回答
0

尝试此代码进行适当的更改。以下代码对我有用。

  • (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {

    静态 NSString *identifier = @"MyLocation";
    if ([annotation isKindOfClass:[MNMyLocation class]]) {

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [atmLocatorMap dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    } else {
        annotationView.annotation = annotation;
    }
    
    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
    //annotationView.image=[UIImage imageNamed:@"arrest.png"];
    
    return annotationView;
    

    }

    返回零;

} `

于 2012-07-23T16:57:47.933 回答