Domanda

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?

È stato utile?

Soluzione

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

Altri suggerimenti

This might be the problem: When the mapview tries to access the view for user's current location, you are passing your customized view.

Try adding this with your existing implementation.

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

if ([annotation isKindOfClass:[MKUserLocation class]]) {
    return nil;     
}
return yourView;
}

PS: This may not be an issue of overlay view.

Best Regards.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top