iOS MapKit - showAnnotations 以在 Zoom 中包含用户位置

iOS MapKit - showAnnotations to Include User Location on Zoom

我在 viewDidAppear 中有一个网络调用,它检索包含要在地图上显示的位置信息的站点列表。调用 showAnnotations 时,地图将缩放以显示所有注释不包括 用户位置。

其他 Stack Overflow 问题说调用带有动画标志的 showAnnotations 为真,但这不起作用。

// Assign new sites
self.sites = newSites

// Show annotations on map
self.mapView.showAnnotations(self.sites, animated: true)

为什么这只会缩放以显示网站而不显示用户的位置?

问题是 showAnnotations 会将注释添加到地图并且只缩放以显示这些注释 - 这非常有意义。那么我们如何包含用户的位置呢?我用下面的代码做到了:

// Assign new sites
self.sites = newSites

// Add new annotations
self.mapView.addAnnotations(self.sites)

// Show all annotations to include current user location
self.mapView.showAnnotations(self.mapView.annotations, animated: true)

这里的关键部分是首先使用 mapView.addAnnotations 添加注释,然后在包含用户位置的所有 mapView 注释上调用 mapView.showAnnotations(..., animated: true)。