ios - 将 MKMap 分解为图 block ——图 block 太小

标签 ios objective-c mkmapview mapkit

我正在使用 MKOverlayMKOverlayViewMKMapView 制作叠加层。首先,我只想根据当前的缩放级别将世界划分为图 block 。因此,当我缩小时,我只想有 1 个大图 block ,下一个缩放级别为 4 个图 block ,然后是 9 个图 block ,等等。这是有效的。现在我的问题是:

在缩放级别 3 上,图 block 之间开始出现间隙。这种情况不会发生在只有 4 个图 block 的缩放级别 2 上,但会发生在随后的每个缩放级别中。

 _ _ _
|1 2 3|
|4 5 6| <-- Tiles
|7_8_9|

这两个图像分别显示图 block 1、2、4、5 和 5、6、8、9。

enter image description here enter image description here

如您所见,每个图 block 后间隙都会增加。现在我的代码:

绘制 map 矩形:

- (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);
}

getRectForMapPoint:

- (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;
}

最佳答案

常见的平铺方式是将每个平铺分为 4 个。因此顶层有 1 个平铺。将其切成 4 份,然后就是下一层。将其中的每一个切成 4 个,您就得到了下一层,等等。这样,任何层上的图 block 都可以非常轻松地计算出其上方或下方图 block 的边界,如果某个层的数据尚未准备好,则可以轻松计算出其上方或下方图 block 的边界。可以使用上面图 block 的一部分,或者连接下面的四个图 block 并使用它。

关于ios - 将 MKMap 分解为图 block ——图 block 太小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17061903/

相关文章:

ios - 拖动放置在 UIStackView 上的项目

ios - 核心数据 N :M relationships

swift - 是否可以自定义 MKUserTrackingBarButtonItem?

iphone - 删除/添加注释到 map View 会导致内存泄漏

ios - 如何获取 GMSMarker X 和 y 坐标 iOS?

ios - iOS 7 中的 NSDateFormatter 字符串

ios - 修改 IBM MobileFirst 7.1 为混合 iOS 应用程序创建的权利文件

objective-c - OSX 多个 nswindow 位于另一个窗口中

ios - 使用 viewDidLayoutSubviews 时出现问题

ios - 滚动时隐藏tableViewCell中的复选标记