当地图上添加了多个叠加层时,我遇到了自定义 MKOverlayView 和标准 MKPolygonView 在某些缩放级别上被裁剪的问题。
阿尔及利亚在两次双击缩放级别上的叠加。 https://oneironautics.org/images/map2.png
阿尔及利亚在三双击缩放级别的叠加。注意剪辑。 https://oneironautics.org/images/map3.png
一些观察:
- 无论我是否使用自定义 MKOverlayView 或返回具有相同多边形的 MKPolygonView,都会发生这种情况。
- 如果我只绘制一个覆盖层,则不会出现此问题。
- 并非所有叠加层都会出现这种情况 - 仅部分叠加层会出现这种情况。
就代码而言:这会将覆盖层添加到 NSMutableArray (borderOverlays),然后在其他地方访问该覆盖层以加载特定国家/地区 ID 的覆盖层。 minX/minY/maxX/maxY 是纬度/经度值;多边形是从 ESRI 形状文件构造的路径。
CLLocationCoordinate2D mbrMin = CLLocationCoordinate2DMake(minY, minX);
CLLocationCoordinate2D mbrMax = CLLocationCoordinate2DMake(maxY, maxX);
MKMapPoint minPoint = MKMapPointForCoordinate(mbrMin);
MKMapPoint maxPoint = MKMapPointForCoordinate(mbrMax);
MKMapSize size = MKMapSizeMake(maxPoint.x - minPoint.x, maxPoint.y - minPoint.y);
MKMapRect rect = MKMapRectMake(minPoint.x, minPoint.y, size.width, size.height);
if ( spans180 ) {
rect = MKMapRectMake(minPoint.x, minPoint.y, MKMapSizeWorld.width * 2, size.height);
}
CustomMKOverlay* overlay = [[CustomMKOverlay alloc] initWithPolygon:polygon withBoundingMapRect:rect];
[borderOverlays addObject:overlay];
叠加层通过以下方式添加到地图中:
[mapView addOverlay:overlay];
视图覆盖:
- (MKOverlayView *)mapView:(MKMapView*)aMapView viewForOverlay:(id<MKOverlay>)overlay
{
if ( [overlay isKindOfClass:[CustomMKOverlay class]] ) {
/* Note: the behaviour if this chunk is not commented is the exact same as below.
CustomMKOverlayView* overlayView = [[[CustomMKOverlayView alloc] initWithOverlay:overlay withMapView:aMapView] autorelease];
[borderViews addObject:overlayView];
return overlayView; */
MKPolygonView* view = [[[MKPolygonView alloc] initWithPolygon:((CustomMKOverlay*)overlay).polygon] autorelease];
view.fillColor = [((CustomMKOverlay*)overlay).colour colorWithAlphaComponent:0.5f];
view.lineWidth = 5.0f;
view.strokeColor = [UIColor blackColor];
[borderViews addObject:view];
return view;
}
}
当使用 MKPolygonView 时,没有绘制代码(所示示例)。不过,为了完整起见,这是我的自定义绘图代码,并且出现了相同的问题。轮廓通常会绘制 - 这实际上是调试绘图,它在覆盖层的boundingMapRect周围绘制一个矩形并填充它,而不用破坏轮廓。
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
CustomMKOverlay* overlay = (CustomMKOverlay*)self.overlay;
CGRect clipRect = [self rectForMapRect:overlay.boundingMapRect];
CGContextAddRect(context, clipRect);
CGContextClip(context);
UIColor* colour = [UIColor redColor];
colour = [colour colorWithAlphaComponent:0.5f];
CGContextSetFillColorWithColor(context, [colour CGColor]);
CGRect fillRect = [self rectForMapRect:overlay.boundingMapRect];
CGContextFillRect(context, fillRect);
}
可以说,我在这一点上有点困惑 - 几乎就像正在加载的缩放平铺图绘制在覆盖层上一样。我已经介绍了有关 TileMap 和 HazardMap 的各种示例,但由于我没有加载自己的地图图块,因此它们不是很有帮助。
我可能错过了一些非常明显的事情。任何帮助,将不胜感激。如果需要,我很乐意提供更多代码/上下文。