【发布时间】: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)
}
}
【问题讨论】: