自定义MKOverlayView /未经修改的MKPolygonView在某些缩放级别被剪辑

我有一个自定义的MKOverlayView和标准的MKPolygonView被裁剪在某些缩放级别的问题,当有多个叠加添加到地图。

阿尔及利亚在两个双击缩放级别叠加。

阿尔及利亚在三个双击缩放级别的叠加。 注意裁剪。

几点意见:

  • 无论是否使用自定义MKOverlayView或返回具有相同多边形的MKPolygonView,都会发生这种情况。
  • 如果我只绘制一个覆盖,这个问题不会发生。
  • 这不会发生在所有覆盖 – 只有一些。

至于代码:这将覆盖添加到一个NSMutableArray(borderOverlays),然后访问其他地方加载覆盖特定的国家ID。 minX / minY / maxX / maxY是纬度/经度值; 多边形是由ESRI shapefile构build的path。

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]; 

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时,没有绘制代码​​(示例显示)。 为了完成,虽然,这是我的自定义绘图代码,并发生同样的问题。 轮廓通常是绘制 – 这实际上是debugging绘图,绘制覆盖的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的各种示例,但是由于我没有加载自己的地图块,所以它们不是很有帮助。

我可能错过了一些显而易见的东西。 任何帮助,将不胜感激。 如果需要,我很乐意提供更多的代码/上下文。

看来罪魁祸首是:

 CLLocationCoordinate2D mbrMin = CLLocationCoordinate2DMake(minY, minX); CLLocationCoordinate2D mbrMax = CLLocationCoordinate2DMake(maxY, maxX); 

MKOverlays的边界矩形显然需要基于边界区域的西北/东南坐标,而不是西南/东北(这是ESRI shapefile存储边界坐标的格式)。 更改有问题的代码为:

 CLLocationCoordinate2D mbrMin = CLLocationCoordinate2DMake(maxY, minX); CLLocationCoordinate2D mbrMax = CLLocationCoordinate2DMake(minY, maxX); 

似乎解决所有问题与缩放和奇怪的轮廓exception。 我希望这可以帮助那些将来遇到这个问题的人(如果不这样做,我希望听到这个消息,因为这个解决scheme对我来说是一种享受)。

另外:如果任何人可以指出任何说明这一点的文件,我想看看它。