如何通过从瓷砖获取alpha来检测建筑物水龙头?

时间:2013-11-21 00:12:06

标签: ios objective-c mkmapview mkoverlay

我一直试图让我的MKMapView检测是否在带有alpha>的图块上有一个水龙头?我在ObjC和Xcode上都很新,所以这个功能有点过头了。非常感谢所有帮助! 到目前为止,我已经尝试了许多不同的策略,但总是很短暂。我们有自定义类来替换分别实现每个的MKOverlay和MKOverlayView,所以我一直试图在创建它们时抓取它们并将它们保存到数组中,以便以后在触摸地图时在MKMapViewController中引用。

- (NSArray *)tilesInMapRect:(MKMapRect)rect zoomScale:(MKZoomScale)scale
{
NSInteger z = zoomScaleToZoomLevel(scale);

// Number of tiles wide or high (but not wide * high)
NSInteger tilesAtZ = pow(2, z);

NSInteger minX = floor((MKMapRectGetMinX(rect) * scale) / TILE_SIZE);
NSInteger maxX = floor((MKMapRectGetMaxX(rect) * scale) / TILE_SIZE);
NSInteger minY = floor((MKMapRectGetMinY(rect) * scale) / TILE_SIZE);
NSInteger maxY = floor((MKMapRectGetMaxY(rect) * scale) / TILE_SIZE);

NSMutableArray *tiles = nil;

for (NSInteger x = minX; x <= maxX; x++) {
    for (NSInteger y = minY; y <= maxY; y++) {
        // As in initWithTilePath, need to flip y index to match the gdal2tiles.py convention.
        NSInteger flippedY = abs(y + 1 - tilesAtZ);

        NSString *tileKey = [[NSString alloc] initWithFormat:@"%d/%d/%d", z, x, flippedY];
        if ([tilePaths containsObject:tileKey]) {
            if (!tiles) {
                tiles = [NSMutableArray array];
            }

            MKMapRect frame = MKMapRectMake((double)(x * TILE_SIZE) / scale,
                                            (double)(y * TILE_SIZE) / scale,
                                            TILE_SIZE / scale,
                                            TILE_SIZE / scale);

            NSString *path = [[NSString alloc] initWithFormat:@"%@/%@.png", tileBase, tileKey];
            ImageTile *tile = [[ImageTile alloc] initWithFrame:frame path:path];
            [tiles addObject:tile];
            [myTiles addObject:tile];
            [path release];
            [tile release];
        }
        [tileKey release];
    }
}
return tiles;
}

这就是我填充数组的地方,这是一个“类变量”。如果我注释掉[tiles addObject:tile];我得到了绘制地图的背景但没有建筑物所以我认为特别添加这些瓷砖是正确的。 然后在mapviewController手势处理函数中,我检查触摸是否在tile.frame中,这是32个中的8个(如果你点击远离建筑物它可以是0,当你缩放时总变化,但总是得到这看起来像个奇怪的数字。但假装这是正常的,我使用此回答者函数的修改版本检查该点的alpha:how to get the RGBA value of UIImage in the specific clicked point 但是我不知道它是否适用于mapView,就像它对于imageViews一样。我想我可能需要翻译上下文,但我以前从未使用过上下文......

抱歉这么多文字!也许这甚至不可能?如果需要澄清,我会添加更多代码。任何输入都会有所帮助!

0 个答案:

没有答案