0

如何重现?

  1. 渲染MKTileOverlay_MKMapView
  2. MKPolygon在地图视图上添加一个
  3. MKTileOverlay闪烁的内容

我的 Xcode 版本是:13.0 (13A233)。如果我在 iOS 14.2 上构建应用程序,地图视图根本不会闪烁。

我没有找到上传演示 zip 文件的方法。所以我附上下面的演示代码:

import UIKit
import MapKit

class ViewController: UIViewController {

    @IBOutlet var mapView: MKMapView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        mapView.delegate = self
        addTileOverlay()
        
        mapView.setRegion(MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 40, longitude: -100), span: MKCoordinateSpan(latitudeDelta: 20, longitudeDelta: 40)), animated: true)
    }

    @IBAction func addPolygonOverlay() {
        let coordinates = [
            CLLocationCoordinate2D(latitude: 35, longitude: -110),
            CLLocationCoordinate2D(latitude: 35, longitude: -90),
            CLLocationCoordinate2D(latitude: 45, longitude: -90),
            CLLocationCoordinate2D(latitude: 45, longitude: -110)
        ]
        
        let polygon = MKPolygon(coordinates: coordinates, count: coordinates.count)
        mapView.addOverlay(polygon)
    }
    
    func addTileOverlay() {
        let template = "https://tile.openstreetmap.org/{z}/{x}/{y}.png"
        let overlay = MKTileOverlay(urlTemplate: template)
        mapView.addOverlay(overlay, level: .aboveLabels)
    }

}

extension ViewController: MKMapViewDelegate {
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        
        if overlay is MKTileOverlay {
            let tileRenderer = MKTileOverlayRenderer(tileOverlay: overlay as! MKTileOverlay)
            return tileRenderer
        } else if (overlay is MKPolygon) {
            let polygonRenderer = MKPolygonRenderer(polygon: overlay as! MKPolygon)
            polygonRenderer.lineWidth = 4
            polygonRenderer.strokeColor = .red
            return polygonRenderer
        }
        
        return MKOverlayRenderer(overlay: overlay)

    }
}
4

0 回答 0