问题描述
我已经通过将MKTileOverlay
&使用MKTileOverlayRenderer
渲染它.它可以正常工作并正确显示自定义地图.
I have implemented Web Map Service in the MKMapView
by subclassing the MKTileOverlay
& rendering it using MKTileOverlayRenderer
. It works fine and displays the custom map properly.
当我调用mapView.addOverlay(polyLine)
之类的方法来添加Polyline或Polygon时. WMS叠加层被Apple Maps取代.
When I call method like mapView.addOverlay(polyLine)
to add Polyline or Polygon. The WMS overlay gets replaced with the Apple Maps overly.
// 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.
How do I prevent this? I don't want wmsTileOverlay to get replaced while adding polyline or polygon.
推荐答案
我意识到,在绘制多边形之前,我正在删除以前的叠加层,因此当时我正在删除所有叠加层.只是检查叠加层是WMSTileOverlay
,然后不删除它.
I realised that before drawing the polygon I was removing the previous overlays so at that time I was removing all the overlays. Just checking the overlay is WMSTileOverlay
then not removing it.
/// Clears the overlays added by the user.
func clearOverlaysOnMapView() {
for overlay in mapView.overlays {
if !(overlay is WMSTileOverlay) {
mapView.removeOverlay(overlay)
}
}
}
这篇关于在将多边形或折线添加到MKMapView时,防止替换WMS叠加层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!