【发布时间】:2021-09-14 11:29:31
【问题描述】:
我看过类似的问题,但似乎没有什么能准确回答这个问题。
我有一个地图视图,我想在其上添加图钉覆盖,以及折线路线覆盖。我是 SwiftUI 开发人员的新手(这是我的第一个应用程序),因此不胜感激。
目前地图只渲染图钉,而不是叠加层。我假设它们需要放入相同的方法中,但无法弄清楚如何将多个叠加层返回到 mapView。
这是协调器代码:
class Coordinator: NSObject,MKMapViewDelegate{
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
//draw pins
if annotation.isKind(of: MKUserLocation.self){return nil}
else{
let pinAnnotation = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "PIN_VIEW")
pinAnnotation.tintColor = .red
pinAnnotation.animatesDrop = true
pinAnnotation.canShowCallout = true
return pinAnnotation
}
}
private func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKPolylineRenderer? {
//draw route
let render = MKPolylineRenderer(overlay: overlay)
render.strokeColor = .orange
render.lineWidth = 2
return render;
}
}
这是输出:(未绘制路线)。我试图将注释放在同一个方法中,但无法弄清楚如何同时返回两者(引脚类型为 MKAnnotationView,路由类型为 MKPolylineRenderer...
非常感谢您的帮助。谢谢
【问题讨论】:
-
另外,您还展示了一半的代码。上次我发布链接 How do I ask a good question? 和 How to create a Minimal, Reproducible Example 时你生气了,但如果你遵循这些指南,你会得到更好的答案。您还没有显示您实际在哪里为地图提供了线条的坐标。没有数据,该函数将永远不会做任何事情。 @Rob 是对的,您需要返回一个 MKRenderer,但还有更多内容,我们无法判断您是否只需要这样做。
标签: swift swiftui mapkit mapkitannotation