Preventing overlays from disappearing when zoomed - MKMapView & MKOverlay

烂漫一生 提交于 2019-12-03 14:12:23
radven

I modified Apple's TileMap sample code by adding an "OverZoom" mode. I have posted more details and my code as an answer to this question.

I hope I can help out anyone else who stumbles across this problem.

MKTileOverlay has a maximumZ property. For me, my tile server only rendered up to zoom 18 (mapnik from osm data), so I set my overlay.maximumZ = 18, and now when I zoom in to 19 or 20 (highest on iPhone), it just keeps the zoom 18 tiles.

That said I am initializing my overlay via initWithURLTemplate:

In viewDidLoad (or wherever you initialize your overlay):

self.mapView.delegate = self;
NSString *urlTemplate = @"http://servername/osm/{z}/{x}/{y}.png";
self.mapOverlay = [[MKTileOverlay alloc] initWithURLTemplate:urlTemplate];
self.mapOverlay.canReplaceMapContent=YES;
[self.map addOverlay:self.overlay level:MKOverlayLevelAboveLabels];

Then implment the following method:

-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {    
    if([overlay isKindOfClass:[MKTileOverlay class]]) {
        MKTileOverlay *tileOverlay = (MKTileOverlay *)overlay;
        tileOverlay.maximumZ = 18;  // This is what sets the cap, zoom levels further in will not be rendered and instead will keep previous zoom level tiles.
        MKTileOverlayRenderer *renderer = [[MKTileOverlayRenderer alloc] initWithTileOverlay:tileOverlay];
        return renderer;
    }
    return nil;
}

I recommend checking out the HazardMap sample code on Apple's site... it's a great example of how to support zooming at all levels with a tiled MKOverlay.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!