Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKOverlay overlays everything with weird circumstances only on ios5?

I have a MKMapView that is having a very strange problem which is kind of hard to explain. I have an MKOverlay that is saved between sessions by saving the points of the overlay to a file and loading the overlay in viewDidLoad. So here's the problem:

Steps

  1. I add an MKOverlay to the MKMapView
  2. On the second or third run after adding the MKOverlay to the map everything on the map gets overlayed by an overlay of the same color as the one I added. Everything on the map with the exception of the view of the map that you can see when the view loaded.

Other weird things:

  1. This problem only occurs when mapView.showsUserLocation is set to true
  2. This problem only occurs on iOS5. Everything works fine on iOS6.
  3. If you zoom out the colored overlay re sizes the visible portion of the map to fill the map at the new level at which you zoomed out to.

This guy is having the same problem, although his solution did not work for me.

Trouble with overlay using MKPolyline & MKPolylineView

Here is some of my code:

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

    MKPinAnnotationView *pinView = nil; //switch this to MKAnnotationView and use .image property if pins need to be images

     if(annotation == self.startingPin || annotation == self.endingPin){

        static NSString *defaultPinID = @"com.MagnusDevelopment.pin";

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

        if (pinView == nil)pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinID];

        pinView.canShowCallout = YES;
        pinView.animatesDrop = TRUE;

        if(annotation == self.startingPin){
        //starting pin
            pinView.pinColor = MKPinAnnotationColorGreen;
        }else{
        //ending pin
            pinView.pinColor = MKPinAnnotationColorRed;
        }

    }
    return pinView;
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKPolylineView *aView = [[MKPolylineView alloc] initWithPolyline:(MKPolyline *)overlay];
    aView.lineWidth = 7.0;
    aView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.6];
    return aView;
}

Code called when the view loads, I have logged the coordinates that are being added to the polyline array right before they are added, and everything is fine and dandy!

    NSInteger numberOfSteps = pointsArray.count;

    NSLog(@"number of steps: %i",numberOfSteps);

    CLLocationCoordinate2D coordinates[numberOfSteps];
    for (NSInteger index = 0; index < numberOfSteps; index++) {
        CLLocation *location = [pointsArray objectAtIndex:index];
        CLLocationCoordinate2D coordinate = location.coordinate;
        coordinates[index] = coordinate;
    }
    MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
    [self.mainMap addOverlay:polyLine];

Here is a picture of what happens when the overlay appears and the user pans to the side on the map: enter image description here Any ideas about what the problem is?

like image 514
Shredder2794 Avatar asked Nov 13 '22 11:11

Shredder2794


1 Answers

You're using dequeueReusableAnnotationViewWithIdentifier, but you're only assigning an annotation to pinView if the result of dequeueReusableAnnotationViewWithIdentifier was nil.

As that method will often return an existing MKPinAnnotationView instance, this should instead be:

MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if (pin == nil) {
    pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
} else {
    pin.annotation = annotation;
}
like image 130
poetmountain Avatar answered Nov 15 '22 06:11

poetmountain