【问题标题】:User Location Focus SwiftUI用户位置焦点 SwiftUI
【发布时间】: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


    【解决方案1】:

    当你按下按钮时,你可以尝试这样的事情,因为你有要关注的坐标:

    func focusLocation(gotoLocation: CLLocationCoordinate2D) {
        let rangeInMeters: Double = 10000   // <-- adjust as desired
        let coordinateRegion = MKCoordinateRegion.init(center: gotoLocation,
                                                       latitudinalMeters: rangeInMeters,
                                                       longitudinalMeters: rangeInMeters)
        mapView.setRegion(coordinateRegion, animated: true)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-23
      • 2021-04-22
      • 2019-10-26
      • 2020-09-11
      相关资源
      最近更新 更多