[英]pins not showing up in mapView. Swift 5
我需要在 map 上显示引脚(注释)。 然后从引脚到引脚绘制多边形线(注释到注释)
我收到一个转换为 CLLocationCoordiante2D 的 Doubles 数组。 第一个 lat 和 long 值始终为 0.0,所以我从数组中删除它们,因为我不希望出现任何问题。
我将 map 双打到坐标,并将它们添加到 mapView
我还包括了一个 viewFor function ,我认为我真的不需要?
map 不会缩放到任何位置,并且没有显示任何引脚。 我知道我需要把它编码进去,我想要一个围绕所有引脚的一般半径。 在别针实际出现后,我会继续努力。
另外,我不在乎名字,我只想让别针出现。
我试过设置一个坐标,但仍然没有别针。
mapView 委托在 viewDidLoad() 中正确设置
我在调试器中记录位置,它们显示正确。
func createAnnotations() {
latitude.remove(at: 0)
longitude.remove(at: 0)
let coordinates = zip(latitude, longitude).map(CLLocationCoordinate2D.init)
AppLogger.logInfo("\(coordinates)")
let annotations = zip(coordinates, names)
.map { (coordinate, name) -> MKPointAnnotation in
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = name
return annotation
}
mapView.addAnnotations(annotations)
mapView.showAnnotations(annotations, animated: true)
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is MKPointAnnotation else { return nil }
let identifier = "Annotation"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.canShowCallout = true
} else {
annotationView!.annotation = annotation
}
return annotationView
}
[__C.CLLocationCoordinate2D(纬度:41.89454659591164,经度:-87.67463844121563),__C.CLLocationCoordinate2D(纬度:41.89424383424124,经度:-87.67461071330482))]
预期的结果是,当显示 mapView 时,我们会看到引脚(注释),以及将它们从第一个引脚连接到最后一个引脚的多边形线。 我可以稍后处理的绘制多边形。
如果您为注释注册注释视图,则不需要viewFor
是正确的,例如
mapView.register(MKPinAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier))
但是,让我们暂时搁置一下。 让我们关注未显示的注释。 有两种可能:
确保您实际上是在添加坐标。
您正在打印coordinates
。 尝试打印annotations
。 即,让我们确保名称不为空,因此注释为空。 如果zip
/ map
是一个非空数组,其中一个为空,则最终结果将为空。 (它被缩短到最小数组长度。)
确保你viewFor
被调用。
假设您已经解决了第一个问题,请尝试在viewFor
中添加断点或日志语句并确保它被调用。 例如,确保您设置了 map 视图的委托等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.