Domanda

For some reason my MKMapView gets an overlay sometimes that covers everything except the view when loaded. You can scroll off to the side after the view loads and view the overlay. Additionally, this problem only exists in ios5 and below, this problem does not occur in ios6. I have found that if you zoom in or out on the map that the overlayed piece of the map will adjust to the new view. For example, if you zoom out then the non-overlayed piece of the map will adjust and get bigger to the new size of your view, but if you then scroll sideways, then the map is still all overlayed in the color defined in the viewForOverlay method. I have no idea why, have been working on this for a while, and Google didn't turn anything up. Here is some of my code:

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {

    if([overlay class] == [MKPolyline class]){
    MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];

    int lineSize = [self.ud integerForKey:@"lineSize"];
    polylineView.lineWidth = lineSize;

    NSString *lineColor = [self.ud objectForKey:@"lineColor"];

    if([lineColor isEqualToString:@"Red"]){
        polylineView.strokeColor = [UIColor redColor];
    }else if([lineColor isEqualToString:@"Blue"]){
        polylineView.strokeColor = [UIColor blueColor];
    }else if ([lineColor isEqualToString:@"Orange"]){
        polylineView.strokeColor = [UIColor orangeColor];
    }else if([lineColor isEqualToString:@"Green"]){
        polylineView.strokeColor = [UIColor greenColor];
    }else if([lineColor isEqualToString:@"Black"]){
        polylineView.strokeColor = [UIColor blackColor];
    }
    return polylineView;
    }else{
        MKOverlayView *overlayToReturn = [[MKOverlayView alloc]initWithOverlay:overlay];
        return overlayToReturn;
    }
}

This is what I get:

MKMapView

È stato utile?

Soluzione

I suspect the problem is with your creation of the MKPolyline or perhaps the lineSize setting. This code works fine here:

#import <MapKit/MapKit.h>
#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    MKMapView *map = [[MKMapView alloc] initWithFrame:self.view.bounds];
    map.delegate = self;
    [self.view addSubview:map];

    CLLocationCoordinate2D coords[] = { {42,-83}, {32, -84}, {45,-78}, {42,-83} };
    MKPolyline *overlay = [MKPolyline polylineWithCoordinates:coords count:4];
    [map addOverlay:overlay];
}

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {

    if([overlay class] == [MKPolyline class]){
    MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];

    int lineSize = 2; //[self.ud integerForKey:@"lineSize"];
    polylineView.lineWidth = lineSize;

    NSString *lineColor = @"Red"; // [self.ud objectForKey:@"lineColor"];

    if([lineColor isEqualToString:@"Red"]){
        polylineView.strokeColor = [UIColor redColor];
    }else if([lineColor isEqualToString:@"Blue"]){
        polylineView.strokeColor = [UIColor blueColor];
    }else if ([lineColor isEqualToString:@"Orange"]){
        polylineView.strokeColor = [UIColor orangeColor];
    }else if([lineColor isEqualToString:@"Green"]){
        polylineView.strokeColor = [UIColor greenColor];
    }else if([lineColor isEqualToString:@"Black"]){
        polylineView.strokeColor = [UIColor blackColor];
    }
    return polylineView;
    }else{
        MKOverlayView *overlayToReturn = [[MKOverlayView alloc]initWithOverlay:overlay];
        return overlayToReturn;
    }
}

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