Подтвердить что ты не робот

Вычисление плиток для отображения в MapRect при "увеличенном увеличении" за пределами набора оверлея

Я работаю над приложением, которое использует представления MKOverlay для размещения моих собственных пользовательских карт поверх базовой карты Google. В качестве руководства я использовал отличный образец кода TileMap Apple (от WWDC 2010).

Моя проблема - когда "overzoomed" до уровня детализации глубже, чем мой сгенерированный набор плиток, код ничего не отображает, потому что на вычисленном уровне Z нет доступных плиток.

Поведение, которое я хочу - когда "overzoomed" приложение должно просто продолжать увеличивать самый глубокий уровень плитки. Хороший пользовательский интерфейс для оверлея становится более грубым - очень плохой опыт, чтобы наложение исчезло.

Вот код, который возвращает фрагменты для рисования - мне нужно выяснить, как это изменить, чтобы ограничить глубину Z, не нарушая масштабирования кадра, который рассчитывается для оверлейной плитки. Любые мысли???


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

    // PROBLEM: I need to find a way to cap z at my maximum tile directory depth.

    // 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];
                [path release];
                [tiles addObject:tile];
                [tile release];
            }
            [tileKey release];
        }
    }

    return tiles;
}

FYI, вот вспомогательная функция zoomScaleToZoomLevel, о которой кто-то спросил:

// Convert an MKZoomScale to a zoom level where level 0 contains 4 256px square tiles,
// which is the convention used by gdal2tiles.py.
static NSInteger zoomScaleToZoomLevel(MKZoomScale scale) {
    double numTilesAt1_0 = MKMapSizeWorld.width / TILE_SIZE;
    NSInteger zoomLevelAt1_0 = log2(numTilesAt1_0);  // add 1 because the convention skips a virtual level with 1 tile.
    NSInteger zoomLevel = MAX(0, zoomLevelAt1_0 + floor(log2f(scale) + 0.5));
    return zoomLevel;
}
4b9b3361

Ответ 1

Представьте, что наложение - это облачный покров - или в нашем случае - покрытие сотового сигнала. Он может не выглядеть "хорошо", а масштабироваться в глубину, но наложение по-прежнему передает важную информацию пользователю.

Я работал над проблемой, добавив режим OverZoom для улучшения кода примера Apple TileMap.

Вот новая функция tilesInMapRect в TileOverlay.m:

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

    // OverZoom Mode - Detect when we are zoomed beyond the tile set.
    NSInteger overZoom = 1;
    NSInteger zoomCap = MAX_ZOOM;  // A constant set to the max tile set depth.

    if (z > zoomCap) {
        // overZoom progression: 1, 2, 4, 8, etc...
        overZoom = pow(2, (z - zoomCap));
        z = zoomCap;
    }

    // When we are zoomed in beyond the tile set, use the tiles
    // from the maximum z-depth, but render them larger.
    NSInteger adjustedTileSize = overZoom * TILE_SIZE;

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

    NSInteger minX = floor((MKMapRectGetMinX(rect) * scale) / adjustedTileSize);
    NSInteger maxX = floor((MKMapRectGetMaxX(rect) * scale) / adjustedTileSize);
    NSInteger minY = floor((MKMapRectGetMinY(rect) * scale) / adjustedTileSize);
    NSInteger maxY = floor((MKMapRectGetMaxY(rect) * scale) / adjustedTileSize);
    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 * adjustedTileSize) / scale,
                                                (double)(y * adjustedTileSize) / scale,
                                                adjustedTileSize / scale,
                                                adjustedTileSize / scale);
                NSString *path = [[NSString alloc] initWithFormat:@"%@/%@.png", tileBase, tileKey];
                ImageTile *tile = [[ImageTile alloc] initWithFrame:frame path:path];
                [path release];
                [tiles addObject:tile];
                [tile release];
            }
            [tileKey release];
        }
    }
    return tiles;
}

И вот новый drawMapRect в TileOverlayView.m:

- (void)drawMapRect:(MKMapRect)mapRect
          zoomScale:(MKZoomScale)zoomScale
          inContext:(CGContextRef)context
{
    // OverZoom Mode - Detect when we are zoomed beyond the tile set.
    NSInteger z = zoomScaleToZoomLevel(zoomScale);
    NSInteger overZoom = 1;
    NSInteger zoomCap = MAX_ZOOM;

    if (z > zoomCap) {
        // overZoom progression: 1, 2, 4, 8, etc...
        overZoom = pow(2, (z - zoomCap));
    }

    TileOverlay *tileOverlay = (TileOverlay *)self.overlay;

    // Get the list of tile images from the model object for this mapRect.  The
    // list may be 1 or more images (but not 0 because canDrawMapRect would have
    // returned NO in that case).

    NSArray *tilesInRect = [tileOverlay tilesInMapRect:mapRect zoomScale:zoomScale];
    CGContextSetAlpha(context, tileAlpha);

    for (ImageTile *tile in tilesInRect) {
        // For each image tile, draw it in its corresponding MKMapRect frame
        CGRect rect = [self rectForMapRect:tile.frame];
        UIImage *image = [[UIImage alloc] initWithContentsOfFile:tile.imagePath];
        CGContextSaveGState(context);
        CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));

        // OverZoom mode - 1 when using tiles as is, 2, 4, 8 etc when overzoomed.
        CGContextScaleCTM(context, overZoom/zoomScale, overZoom/zoomScale);
        CGContextTranslateCTM(context, 0, image.size.height);
        CGContextScaleCTM(context, 1, -1);
        CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), [image CGImage]);
        CGContextRestoreGState(context);

        // Added release here because "Analyze" was reporting a potential leak. Bug in Apple sample code?
        [image release];
    }
}

Кажется, теперь отлично работает.

BTW - я думаю, что в образце кода TileMap отсутствует [релиз изображения] и происходит утечка памяти. Обратите внимание, где я добавил его в код выше.

Я надеюсь, что это поможет некоторым другим с той же проблемой.

Приветствия,

  • Крис

Ответ 2

Этот алгоритм, как представляется, создает много картографических фрагментов вне MapRect. Добавление внутри цикла цикла, чтобы пропускать фрагменты за пределами границ, помогает много:

if (! MKMapRectIntersectsRect(rect, tileMapRect))
   continue;

Ответ 3

Немного поздно для вечеринки, но... В iOS 7.0 и выше вы можете использовать свойство maximumZ на MKTileOverlay. Из документы:

Если вы используете разные объекты наложения для представления разных фрагментов в различные уровни масштабирования, используйте это свойство, чтобы указать максимальный масштаб поддерживаемый этими оверлеями. При уровне масштабирования 0 плитка вся карта мира; при уровне масштабирования 1 плитка покрывает 1/4 мира; на уровне увеличения 2, плитки покрывают 1/16 мира и так далее. Карта никогда не пытается загружать плитки для уровня масштабирования, превышающего значение указанный этим свойством.

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {

    if ([overlay isKindOfClass:[MKTileOverlay class]]) {

        MKTileOverlay *ovrly = (MKTileOverlay *)overlay;
        ovrly.maximumZ = 9;  // Set your maximum zoom level here
        MKTileOverlayRenderer *rndr = [[MKTileOverlayRenderer alloc] initWithTileOverlay:ovrly];
        return rndr;

    }

    return nil;
}