2
在我的應用程序中,我想在MKMapView上顯示註釋,但我不希望用戶看到地圖圖塊,我希望它們是完全黑色的。 我怎樣才能達到這樣的效果?iOS Swift MKMapView刪除地圖圖塊
在我的應用程序中,我想在MKMapView上顯示註釋,但我不希望用戶看到地圖圖塊,我希望它們是完全黑色的。 我怎樣才能達到這樣的效果?iOS Swift MKMapView刪除地圖圖塊
替換地圖圖塊的機制基於MKOverlayRenderer類。您需要製作自己的MKOverlayRenderer子類,您可以在其中自定義地圖圖塊的顯示。 在您使用地圖的ViewController中,您需要爲您的MKMapView添加一個覆蓋層來觸發MKMapViewDelegate的渲染方法。
示例代碼:
//Subclass of MKOverlayRenderer to tint the tiles
class MyCustomOverlayRenderer: MKOverlayRenderer {
override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {
let drawRect = self.rect(for: mapRect)
context.setFillColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
context.fill(drawRect)
}
}
//Custom View Controller where the map can be found
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
self.mapView.delegate = self
let tiles = MKTileOverlay(urlTemplate: nil)
tiles.canReplaceMapContent = true
self.mapView.add(tiles)
}
//Custom method to add an annotation onto your mapView
@IBAction func mapViewTapped(_ sender: Any) {
let annotation = MyCustomAnnotation(coordinate: self.mapView.centerCoordinate, title: "MyAnnotation")
self.mapView.addAnnotation(annotation)
}
}
extension ViewController : MKMapViewDelegate
{
//use your custom renderer to render all tiles
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
return MyCustomOverlayRenderer(overlay: overlay)
}
//show a pin in the middle of you map
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
return nil
}
}
,其結果是: