由于某种原因,我的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;
}
}
这就是我得到的:
答案 0 :(得分: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