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
MKOverlay
to the MKMapView
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:
mapView.showsUserLocation
is set to
trueiOS5
. Everything works fine on iOS6
.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: Any ideas about what the problem is?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With