自定义MKOverlayView/未修改的MKPolygonView被裁剪为某些缩放级别
当在地图上添加了多个叠加层时,自定义MKOverlayView和标准MKPolygonView都被裁剪为某些缩放级别时,我遇到了问题.
I'm having an issue with both a custom MKOverlayView and standard MKPolygonView being clipped at certain zoom levels when there are multiple overlays added to a map.
一些观察结果:
- 无论我是否使用自定义MKOverlayView或返回具有相同多边形的MKPolygonView,都会发生这种情况.
- 如果我仅绘制一个叠加层,则不会发生此问题.
- 并非所有的叠加层都发生这种情况-只有一些.
就代码而言:将叠加层添加到NSMutableArray(borderOverlays)中,然后可以在其他位置访问该叠加层以加载特定国家/地区ID的叠加层. minX/minY/maxX/maxY是纬度/经度值;多边形是从ESRI shapefile构造的路径.
As far as code goes: this adds the overlay to an NSMutableArray (borderOverlays), which is then accessed elsewhere to load the overlay for a specific country ID. minX/minY/maxX/maxY are latitude/longitude values; polygon is a path constructed from an ESRI shapefile.
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];
叠加层通过以下方式添加到地图中:
The overlay is added to the map via:
[mapView addOverlay:overlay];
viewForOverlay:
viewForOverlay:
- (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周围绘制一个rect,并填充它而不会与轮廓混为一谈.
When MKPolygonView is used, there is no drawing code (the example shown). For completion's sake, though, here's my custom drawing code, and the same issue occurs. The outlines normally draw - this is actually debugging drawing, which draws a rect around the boundingMapRect of the overlay and fills it without mucking around with the outlines.
- (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的各种示例,但是由于我没有加载自己的地图图块,因此它们并不是很有帮助.
Suffice to say, I'm a bit stumped at this point - it's almost as if the zoomed tiled that's being loaded draws over the overlay. I've poured over various examples regarding TileMap and HazardMap, but as I am not loading my own map tiles, they're not very helpful.
我可能缺少明显的东西.任何帮助,将不胜感激.如有必要,我很乐意提供更多代码/上下文.
I'm probably missing something painfully obvious. Any help would be appreciated. I'm happy to provide more code/context if necessary.
罪魁祸首是:
CLLocationCoordinate2D mbrMin = CLLocationCoordinate2DMake(minY, minX);
CLLocationCoordinate2D mbrMax = CLLocationCoordinate2DMake(maxY, maxX);
MKOverlays的边界矩形显然需要基于边界区域的西北/东南坐标,而不是西南/东北(这是ESRI shapefile存储其边界坐标的格式).将有问题的代码更改为:
Bounding rectangles for MKOverlays apparently need to be based on the northwest/southeast coordinates of the bounding region, and not southwest/northeast (which is the format the ESRI shapefile stores its bounding coordinates in). Changing the offending code to:
CLLocationCoordinate2D mbrMin = CLLocationCoordinate2DMake(maxY, minX);
CLLocationCoordinate2D mbrMax = CLLocationCoordinate2DMake(minY, maxX);
似乎可以解决缩放和奇怪的轮廓异常的所有问题.我希望这能对以后遇到此问题的任何人有所帮助(并且如果没有解决,我希望听到有关此问题的消息,因为该解决方案对我有效).
Appears to resolve all issues with zooming and strange outline anomalies. I hope this helps anyone who comes across this problem in the future (and I'd like to hear about it if it doesn't, since this solution works a treat for me).
此外:如果任何人都可以指向任何说明此内容的文档,我希望看到它.
Also: if anyone can point to any documentation that states this, I'd like to see it.