简体   繁体   English

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

[英]How to programmatically change map color from day to night in ios7

I am working on an app for iOS 7, and am trying to change the map from day to night and night to day mode. 我正在开发适用于iOS 7的应用程序,并试图将地图从白天更改为夜间,并将夜间更改为白天模式。 I have not found any relevant APIs in iOS 7 documentation to do this. 我没有在iOS 7文档中找到任何相关的API来执行此操作。

This is not a built in feature of MKMapKit so what you are asking is not possible without doing it yourself. 这不是MKMapKit的内置功能,因此如果不自己进行操作,就不可能问您什么。 If you were going to do it yourself, the best you could do would be to find a map tile source of 'night mode' tiles, and use the MKTileOverlay class (New to iOS 7) to replace the content of the map entirely. 如果您要自己进行操作,则最好的办法是找到“夜间模式”图块的地图图块源,并使用MKTileOverlay类(iOS 7的新增功能)完全替换地图的内容。

A brief code example using the Open Street Map tile source (Not night tiles!) 使用“开放街道地图”图块源的简短代码示例(不是夜间图块!)

// 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];

Then implement the mapView delegate method below... 然后在下面实现mapView委托方法...

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM