1

I'm trying to find a reliable method for determining the MKTileOverlayPath of the tiles currently visible in a MKMapView at any given instant (as the user zooms in/out and pans). My application displays dynamic content from a server which is queried by MKTileOverlayPath.

Is there a way to determine this directly from the MKMapView or MKTileOverlay classes?

So far I've been trying to calculate the tilepaths using the code below. It works OK when the user pans or double-clicks to zoom-in. However, when pinching to zoom in/out (where the zoom-level can be continuous) the tiles calculated using this method are sometimes not correct - specifically the y coordinates seem to be one off.

I'm struggling to figure out why the code doesn't work correctly with the pinch zoom levels so I'm wondering if there is an alternative way to determine the currently visible tiles. I've tried implementing a dummy MKTileOVerlay to intercept the requested tiles but this it's not clear from that which are currently in view at any given time.

Any tips would be greatly appreciated.

- (NSMutableArray *)tilesInMapRect:(MKMapRect)rect zoomScale:(MKZoomScale)scale
{

    NSInteger z = [self zoomLevel];

    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++) {

            NSString *tileString = [NSString stringWithFormat:@"z%ix%iy%i",z,x,y];
            if (!tiles) {
                tiles = [NSMutableArray array];
            }
            [tiles addObject:tileString];
        }
    }

    return tiles;
}

- (NSUInteger) zoomLevel {
    return (21 - round(log2(self.mapView.region.span.longitudeDelta * MERCATOR_RADIUS * M_PI / (180.0 * self.mapView.bounds.size.width))));
}
user55674
  • 51
  • 1
  • 4
  • I think there's almost certainly a way to get this information more directly, but for the code you've posted, you probably want `ceil` for at least the maxY and maxX indexes, rather than floor. – Jesse Rusak Oct 30 '13 at 17:11
  • I found the code in this related posting helped fix the problem by calculating the expected visible rect for closest zoom level. [link](http://stackoverflow.com/questions/14341200/strange-behavior-of-mkmapview-visiblemaprect-property/14357319#14357319) – user55674 Nov 01 '13 at 16:20

0 Answers0