0

出于某种原因,我MKMapView有时会得到一个覆盖,覆盖除了加载时的视图之外的所有内容。您可以在视图加载后滚动到一侧并查看叠加层。另外,这个问题只存在于ios5及以下,ios6不会出现这个问题。我发现,如果您放大或缩小地图,地图的覆盖部分将适应新视图。例如,如果您缩小地图,则地图的未覆盖部分将调整并变大以适应您的视图的新大小,但如果您随后横向滚动,则地图仍将全部覆盖在 viewForOverlay 中定义的颜色方法。我不知道为什么,已经为此工作了一段时间,但谷歌没有发现任何问题。这是我的一些代码:

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

这就是我得到的:

MKMapView

4

1 回答 1

1

我怀疑问题出在您创建 MKPolyline 或 lineSize 设置上。这段代码在这里工作正常:

#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
于 2013-01-05T23:34:07.853 回答