如何在用户移动时更新 MKMapView 上的图钉

How do I update pins on a MKMapView as a user moves

我在数据库中查询商店信息。我希望商店出现在用户当前位置周围一定半径范围内的屏幕上。我希望新商店随着用户移动而出现在屏幕上,而无需用户手动刷新任何内容。我应该使用什么委托方法来查询商店并将它们放置在屏幕上?

您可以使用centerCoordinate

获取地图中心点的坐标
 var coord: CLLocationCoordinate2D = CLLocationCoordinate2DMake(map.centerCoordinate.latitude, map.centerCoordinate.longitude)

您可以查看 Tracking the User Location 的 MKMapView 使用其委托。

用于位置更新 MKMapViewDelagate

- mapView:didUpdateUserLocation:

要将它们放在屏幕上,请删除以前的注释并添加从您的查询中获取的新注释。

你只需要实现 func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? 并使用 CLLocation 监听位置变化 Manager.Whenever 新位置出现查询兴趣点并通过 addAnnotations 附加到地图视图。之后设置地图视图中心坐标属性 在地图视图中显示用户当前位置