【发布时间】:2021-09-10 03:02:13
【问题描述】:
我正在尝试编写一个 ToDo 应用程序,并希望将整个事情设计为一张地图,在该地图上,ToDo 列表上的各个点显示为图钉,并且每次到达这些图钉中的一个时都应该执行一个事件.为了测试整个事情,我采用了一个 GPS 轨迹,它应该模拟公共汽车,并为我的应用程序提供 GPS 数据。各个点都在这个 GPS 轨迹上。
我的应用有一个按钮,当您按下它时,它始终关注用户的位置。问题是他总是关注 GPS 轨迹的起点,而不是 GPS 轨迹的当前位置。
非常感谢您的帮助!
这是焦点函数和位置更新函数
@Published var region : MKCoordinateRegion!
func focusLocation() {
guard let _ = region else{return}
mapView.setRegion(region, animated: true)
mapView.setVisibleMapRect(mapView.visibleMapRect, animated: true)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else{return}
self.region = MKCoordinateRegion(center: location.coordinate, latitudinalMeters: 10000,
longitudinalMeters: 10000)
self.mapView.setRegion(self.region, animated: true)
self.mapView.setVisibleMapRect(self.mapView.visibleMapRect, animated: true)
}
【问题讨论】:
标签: swiftui mapkit core-location