作者:周茜闹心_325 | 来源:互联网 | 2023-10-11 21:31
我已经在MKMapView
中实现了Web Map Service,方法是将MKTileOverlay
子类化并使用MKTileOverlayRenderer
进行渲染。它可以正常工作并正确显示自定义地图。
当我调用mapView.addOverlay(polyLine)
之类的方法来添加Polyline或Polygon时。 WMS叠加层被替换为Apple Maps。
// Set up the overlay and adds it to MKMapView.
func setupTileRenderer() {
let wmsURL = formTemplate?.wmsURL
let overlay = WMSTileOverlay(urlTemplate: wmsURL)
overlay.canreplaceMapCOntent= true
mapView.addOverlay(overlay,level: .aboveLabels)
tileRenderer = MKTileOverlayRenderer(tileOverlay: overlay)
wmsTileOverlay = overlay
}
func mapView(_ mapView: MKMapView,rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKPolyline {
let render = MKPolylineRenderer(overlay: overlay)
render.lineWidth = 2
render.strokeColor = UIColor.red
return render
} else if overlay is MKPolygon {
let render = MKPolygonRenderer(overlay: overlay)
render.lineWidth = 2
render.strokeColor = UIColor.red
return render
} else if overlay is WMSTileOverlay {
return tileRenderer!
}
return MKOverlayRenderer(overlay: overlay)
}
如何防止这种情况?我不希望在添加折线或多边形时替换wmsTileOverlay。
我意识到在绘制多边形之前,我要删除以前的叠加层,所以当时我要删除所有叠加层。只需检查叠加层即可WMSTileOverlay
,然后再将其删除。
/// Clears the overlays added by the user.
func clearOverlaysOnMapView() {
for overlay in mapView.overlays {
if !(overlay is WMSTileOverlay) {
mapView.removeOverlay(overlay)
}
}
}