SwiftUI MapKit - 同一张地图中的多个注释
SwiftUI MapKit - Multiple annotations in same map
我看过类似的问题,但似乎没有什么能准确回答这个问题。
我有一个地图视图,我想添加图钉覆盖图,以及在其上添加多线路线覆盖图。我是 SwiftUI 开发人员的新手(这是我的第一个应用程序),因此不胜感激。
目前地图只渲染图钉,不渲染叠加层。我假设它们需要放入相同的方法中,但无法弄清楚如何 return 将多个叠加层叠加到 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;
}
}
输出如下:(未绘制路线)。我试图将注释放在同一个方法中,但无法弄清楚如何同时 return 两者(引脚类型为 MKAnnotationView,路由类型为 MKPolylineRenderer...
非常感谢您的帮助。谢谢
应该是一个单独的方法,但不应该是private
。此外,return 类型应为 MKOverlayRenderer
.
我看过类似的问题,但似乎没有什么能准确回答这个问题。
我有一个地图视图,我想添加图钉覆盖图,以及在其上添加多线路线覆盖图。我是 SwiftUI 开发人员的新手(这是我的第一个应用程序),因此不胜感激。
目前地图只渲染图钉,不渲染叠加层。我假设它们需要放入相同的方法中,但无法弄清楚如何 return 将多个叠加层叠加到 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;
}
}
输出如下:(未绘制路线)。我试图将注释放在同一个方法中,但无法弄清楚如何同时 return 两者(引脚类型为 MKAnnotationView,路由类型为 MKPolylineRenderer...
非常感谢您的帮助。谢谢
应该是一个单独的方法,但不应该是private
。此外,return 类型应为 MKOverlayRenderer
.