App crashing when I add overlay to mkmapview

孤街浪徒 提交于 2019-12-07 04:34:57

问题


I am trying to add annotations and overlays to a mapview but it crashes randomly. It is an EXC_BAD_ACCESS error, but zombies doesn't tell me anything. It says it is crashing on CG::Path::apply_transform(CGAffineTransform const&). I have looked everywhere for why this is happening but can't pinpoint it.

I am creating the mapview in ib and have the delegates and everything set up right. It will work sometimes and then crash randomly. I am using a gesture recognizer to add the annotations and overlay

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

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

and

- (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];


    }

}

And the views for each:

-(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;
}

Is there a better way to add annotations/overlays to maps with user interaction? Am I doing something wrong in this code? It appears to draw most of the circle but then crashes... Is there some special trick to mapviews?


回答1:


I've been getting the exact same error:

CG::Path::apply_transform(CGAffineTransform const&) will hit a test instruction and give me an EXC_BAD_ACCESS

This specifically occurs when using a double click on the map to zoom in on an MKCircle.

I can't say this definitively, but to the best of my knowledge this problem only occurs on the simulator when you use a double click to zoom, I've never been able to cause the error from an actual device, or by using option+click to zoom on the simulator.

So at this point I've filed this under "simulator bug" and left it at that.

If you do discover anything to the contrary, please let me know because it really bothers me not explicitly knowing whether or not this is a bug existing in my app that I just can't properly reproduce.

edit:

This was flagged initially as "not an answer" so I'll provide a little bit more information supporting my conjecture.

Basically in both of our scenarios a gesture is firing the re-rendering of the MKCircleView, what I strongly suspect is that since the simulator is able to generate some kind of gesture which can't be created from a user on the actual device, there is a failed expectation somewhere down the chain while that gesture gets handled.




回答2:


I am not sure where your EXC_BAD_ACCESS problems is. But you have a big problem with leaking memory. You have to releas object that you create with init. In the above code you create objects and never release them. That will not throw EXC_BAD_ACCESS but it will consume your memory.

Release the following object :

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

MKCircleView* circleView = [[MKCircleView alloc] initWithOverlay:overlay];


来源:https://stackoverflow.com/questions/6721124/app-crashing-when-i-add-overlay-to-mkmapview

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