我正在研究MKMapView
使用MKOverlay
and的叠加层MKOverlayView
。首先,我只想根据当前的缩放级别将世界划分为图块。因此,当我缩小时,我只想拥有 1 个大图块,下一个缩放级别为 4 个图块,然后是 9 个等。这是有效的。现在我的问题:
在 zoomLevel 3 上,图块之间开始出现间隙。这不会发生在只有 4 个 Tiles 的缩放级别 2 上,而是在每个后续缩放级别中发生。
_ _ _
|1 2 3|
|4 5 6| <-- Tiles
|7_8_9|
这两个图像分别显示了瓷砖 1、2、4、5 和 5、6、8、9。
正如您所看到的,每个瓷砖后间隙都会增加。现在到我的代码:
绘制地图矩形:
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
NSUInteger zoomLevel = [self zoomLevelForZoomScale:zoomScale];
HeatMap *heatMap = (HeatMap *)self.overlay;
HeatMapTileManager *tileManager = [heatMap getHeatMapTileManagerForZoomLevel:zoomLevel];
MKMapRect mapTile = [tileManager getRectForMapPoint:mapRect.origin atZoomLevel:zoomLevel];
CGContextSetAlpha(context, 0.5);
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGColorRef color = CGColorCreate(rgb, (CGFloat[]){ .745, .941, .467, 1 });
CGContextSetFillColorWithColor(context, color);
CGRect mapTileCGRect = [self rectForMapRect:mapTile];
CGContextFillRect(context, mapTileCGRect);
}
获取矩形映射点:
- (MKMapRect) getRectForMapPoint:(MKMapPoint)mapPoint
atZoomLevel:(NSInteger)level
{
double stepSize = MKMapSizeWorld.width / (double)level;
double rectIDx = floor(mapPoint.x / stepSize);
double rectIDy = floor(mapPoint.y / stepSize);
MKMapRect mapRect = MKMapRectMake(stepSize * rectIDx,
stepSize * rectIDy,
stepSize,
stepSize);
NSLog(@"X: %f, Width: %f", mapRect.origin.x, stepSize);
NSLog(@"Y: %f, Height: %f", mapRect.origin.y, stepSize);
return mapRect;
}