当我向mkmapview添加叠加层时应用崩溃

时间:2011-07-17 00:17:40

标签: iphone objective-c mkmapview mkoverlay

我正在尝试向地图视图添加注释和叠加层,但它会随机崩溃。这是一个EXC_BAD_ACCESS错误,但僵尸并没有告诉我任何事情。它说它崩溃在CG :: Path :: apply_transform(CGAffineTransform const&)上。我到处都看到为什么会这样,但无法确定它。

我正在ib中创建mapview并让代表和所有设置正确。它会工作,然后随机崩溃。 我正在使用手势识别器添加注释和叠加

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] 
                                     initWithTarget:self action:@selector(handleDoubleTap:)];

[doubleTap setNumberOfTapsRequired:2];
[self.mapView addGestureRecognizer:doubleTap];

- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer
{

    if (gestureRecognizer.state == UIGestureRecognizerStateRecognized){ 
        CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];   
        CLLocationCoordinate2D touchMapCoordinate = 
        [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];


        //add pin where user touched down...
        MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
        pa.coordinate = touchMapCoordinate;
        //[pa setTitle:@"title"];
        [mapView addAnnotation:pa];

        MKCircle* circle=[MKCircle circleWithCenterCoordinate:touchMapCoordinate radius:500];
        [mapView addOverlay:circle];


    }

}

每个人的观点:

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay 
{
    if ([overlay isKindOfClass:[MKCircle class]]) {
        MKCircleView* circleView = [[MKCircleView alloc] initWithOverlay:overlay];
        circleView.strokeColor = [UIColor redColor];
        circleView.lineWidth = 1.0;
        circleView.fillColor = [UIColor blackColor];
        circleView.alpha=.5;
        return circleView;

    }
    else
        return nil;

}


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

    if (![annotation isKindOfClass:[MKUserLocation class]]) {
        static NSString *AnnotationIdentifier = @"Annotation";
        MKPinAnnotationView* pinView = (MKPinAnnotationView *)[localmapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
        if (!pinView)
        {
            pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
            pinView.pinColor = MKPinAnnotationColorRed;
            pinView.animatesDrop = YES;
        }
        else
        {
                pinView.annotation = annotation;
            }
            return pinView;

        }
        return nil;
}

是否有更好的方法通过用户互动向地图添加注释/叠加层?我在这段代码中做错了吗?它似乎绘制了大部分圆圈但随后崩溃了... mapviews有一些特殊的技巧吗?

2 个答案:

答案 0 :(得分:4)

我一直得到完全相同的错误:

CG :: Path :: apply_transform(CGAffineTransform const&amp;)将点击测试指令并给我一个EXC_BAD_ACCESS

当在地图上双击放大MKCircle时,会发生这种情况。

我不能肯定地说这个,但据我所知,这个问题只发生在模拟器上,当你使用双击缩放时,我从来没有能够从实际设备引起错误,或者通过使用选项+单击可放大模拟器。

所以在这一点上,我已经在“模拟器错误”下提交了这个并将其保留在那里。

如果您确实发现了相反的情况,请告诉我,因为我真的很不知道这是不是我的应用程序中存在的错误,我无法正确复制。

编辑:

最初标记为“不是答案”,因此我将提供更多信息支持我的推测。

基本上在我们的两个场景中,一个手势触发了MKCircleView的重新渲染,我强烈怀疑的是,因为模拟器能够生成某种无法从用户实际创建的手势设备,当处理该手势时,在链的某处有一个失败的期望。

答案 1 :(得分:-4)

我不确定您的EXC_BAD_ACCESS问题在哪里。但是你的内存泄漏存在很大问题。您必须释放使用init创建的对象。在上面的代码中,您创建对象并且永远不会释放它们。这不会抛出EXC_BAD_ACCESS,但它会消耗你的内存。

发布以下对象:

MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];

MKCircleView* circleView = [[MKCircleView alloc] initWithOverlay:overlay];
相关问题