Smi*_*ith 5 objective-c mapkit ios
我有一个问题,当有多个叠加添加到地图时,自定义MKOverlayView和标准MKPolygonView被剪裁在某些缩放级别.
一些观察:
就代码而言:这会将叠加层添加到NSMutableArray(borderOverlays),然后在其他位置访问以加载特定国家/地区ID的叠加层.minX/minY/maxX/maxY是纬度/经度值; polygon是从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];
Run Code Online (Sandbox Code Playgroud)
叠加层通过以下方式添加到地图中:
[mapView addOverlay:overlay];
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
使用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);
}
Run Code Online (Sandbox Code Playgroud)
我只想说,在这一点上我有点难过 - 这几乎就像正在加载的缩放平铺覆盖了叠加层.我已经倾倒了关于TileMap和HazardMap的各种示例,但由于我没有加载自己的地图图块,所以它们并没有太大帮助.
我可能错过了一些非常明显的东西.任何帮助,将不胜感激.如有必要,我很乐意提供更多代码/上下文.
看来罪魁祸首是:
CLLocationCoordinate2D mbrMin = CLLocationCoordinate2DMake(minY, minX);
CLLocationCoordinate2D mbrMax = CLLocationCoordinate2DMake(maxY, maxX);
Run Code Online (Sandbox Code Playgroud)
MKOverlays 的边界矩形显然需要基于边界区域的西北/东南坐标,而不是西南/东北(这是 ESRI shapefile 存储其边界坐标的格式)。将有问题的代码更改为:
CLLocationCoordinate2D mbrMin = CLLocationCoordinate2DMake(maxY, minX);
CLLocationCoordinate2D mbrMax = CLLocationCoordinate2DMake(minY, maxX);
Run Code Online (Sandbox Code Playgroud)
似乎解决了缩放和奇怪的轮廓异常的所有问题。我希望这对将来遇到这个问题的人有所帮助(如果没有,我想听听,因为这个解决方案对我来说是一种享受)。
另外:如果有人可以指出任何说明这一点的文档,我希望看到它。
| 归档时间: |
|
| 查看次数: |
729 次 |
| 最近记录: |