【问题标题】:SwiftUI Update coordinate location constantlySwiftUI 不断更新坐标位置
【发布时间】:2020-08-10 03:39:28
【问题描述】:

我正在尝试模仿一个类似于超级汽车的功能,您可以在该功能中看到一个大头针在地图上不断移动。

我有一张地图,上面有别针。现在,我可以设置一个循环并在计时器上为其提供不同的坐标,在此我等待第二个中间坐标。

或者有比在 updateUIView 函数中更好的方法吗?

我知道坐标应该是一个@State 变量,但我应该在哪里更改变量?我应该调用另一种方法吗?我应该在哪里调用它?

struct ContentView: UIViewRepresentable {


func makeUIView(context: Context) -> MKMapView {
    MKMapView(frame: .zero)
}

func updateUIView(_ view: MKMapView, context: Context) {


    // 1
    view.mapType = MKMapType.standard // (satellite)

    // 2
    let mylocation = CLLocationCoordinate2D(latitude: 47.6127177,longitude: -122.1995211)

    // 3
    let runner = CLLocationCoordinate2D(
        latitude: 47.611991,longitude: -122.198849)
    let span = MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005)
    let region = MKCoordinateRegion(center: mylocation, span: span)
    view.setRegion(region, animated: true)

    // 4
    let annotation = MKPointAnnotation()
    annotation.coordinate = mylocation

    annotation.title = "My Location"
    
    let annotation_moving = MKPointAnnotation()
    annotation_moving.coordinate = runner
    annotation_moving.title = "ubercare"

    
    let annotations = [annotation, annotation_moving]
    view.addAnnotations(annotations)
    
    

}
}

【问题讨论】:

    标签: swift swiftui mapkit


    【解决方案1】:

    您最好创建一个对象/类,使其成为这种类型的Coordinator。然后您可以设置CADisplayLink 或其他一些基于计时器的机制来更新坐标。但是你的协调器是一个类也意味着它不会在每次View 层次结构中的某些东西发生变化时都被吹走,这对你的情况很重要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 2014-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多