【问题标题】:iOS Swift MKMapView remove map tilesiOS Swift MKMapView 删除地图图块
【发布时间】:2017-01-06 19:50:42
【问题描述】:

在我的应用程序中,我想在 MKMapView 上显示注释,但我不希望用户看到地图图块,我希望它们完全是黑色的。 我怎样才能实现这种外观?

【问题讨论】:

    标签: ios mkmapview mapkit


    【解决方案1】:

    替换地图瓦片的机制基于 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
        }
    }
    

    结果是:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-06
      • 1970-01-01
      • 1970-01-01
      • 2022-12-16
      • 1970-01-01
      • 2016-06-21
      • 1970-01-01
      • 2011-05-21
      相关资源
      最近更新 更多