如何在 ios7 中以编程方式将地图颜色从白天更改为黑夜



我正在开发一款适用于iOS 7的应用程序,并尝试将地图从白天更改为夜间,从夜间更改为白天模式。我在iOS 7文档中没有找到任何相关的API来执行此操作。

这不是

MKMapKit的内置功能,因此如果不自己做,您所要求的是不可能的。如果你打算自己做,你能做的最好的事情就是找到"夜间模式"图块的地图图块源,并使用MKTileOverlay类(iOS 7 的新功能)完全替换地图的内容。

使用开放街道地图磁贴源的简短代码示例(不是夜间磁贴!

// Put this in your -viewDidLoad method
NSString *template = @"http://tile.openstreetmap.org/{z}/{x}/{y}.png";
MKTileOverlay *overlay = [[MKTileOverlay alloc] initWithURLTemplate:template];
//This is the important bit, it tells your map to replace ALL the content, not just overlay the tiles.
overlay.canReplaceMapContent = YES;
[self.mapView addOverlay:overlay level:MKOverlayLevelAboveLabels];

然后实现下面的mapView委托方法...

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
    if ([overlay isKindOfClass:[MKTileOverlay class]]) {
        return [[MKTileOverlayRenderer alloc] initWithTileOverlay:overlay];
    }
}

有关完整参考,请参阅 https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKTileOverlay_class/Reference/Reference.html

最新更新