【问题标题】:How to animate moving of MKOverlay on MKMapView Swift如何在 MKMapView Swift 上动画 MKOverlay 的移动
【发布时间】:2019-03-22 05:56:30
【问题描述】:

我正在开发一个地图应用程序,在该应用程序中我必须根据用户位置为飞机的运动制作动画

所以我通过子类化MKOverlay 实现了一个自定义类MKPlaneAndArrow

class MKPlaneAndArrow : NSObject, MKOverlay {

  //@objc dynamic var coordinate: CLLocationCoordinate2D
  //the above method also does not change location

  //to change the coordinate after initialization
  var coordinate: CLLocationCoordinate2D {
    willSet {
      willChangeValue(forKey: #keyPath(coordinate))
    }
    didSet {
      didChangeValue(forKey: #keyPath(coordinate))
    }
  }

  var boundingMapRect: MKMapRect

  init( center: CLLocationCoordinate2D, boundingMapRect_: MKMapRect) {

    self.coordinate = center
    self.boundingMapRect = boundingMapRect_

  }

}

现在,当位置委托didUpdateLocations 被调用时,我想将MKPlaneAndArrow 覆盖的坐标更改为当前位置坐标,所以我在didUpdateLocations 中写了以下内容

    //if not already added annotation
if !HomeVC.flagAlreadyAddedPlaneAnnotation {

    let planeAndArrow = MKPlaneAndArrow.init(center: lastUserLocation.coordinate, boundingMapRect_: mapRect)
    mapView.add(planeAndArrow)
    //Did updateLocations will not add the annotation again
    HomeVC.flagAlreadyAddedPlaneAnnotation = true

}
//get last user location
guard let lastUserLocation:CLLocation = locations.last  else {
    print("no location")
    return
}
 //change coordinate of `MKPlaneAndArrow` overlay
for overlay in mapView.overlays {

    if let myPlane = overlay as? MKPlaneAndArrow {
     myPlane.coordinate = lastUserLocation.coordinate 
    }
}

我已经设置了断点,它们到达了myPlane.coordinate = ... 但我不知道为什么地球上的坐标变化在地图上不可见


动画

如果上面的代码改变了坐标,它不会动画变化。我也想让它动画。比如在谷歌地图或苹果地图上移动用户位置

我成功地用动画改变了 MKAnnotation 的坐标。但是通过叠加,我已经搜索了几个小时但找不到解决方案

我看到以下关于 SO 的问题以及许多其他问题

注意事项:

  • 无法使用MkAnnotation,因为如果您缩放,它们会保持相同的大小,而叠加层的大小与缩放比例有关

  • 如果您需要我的MKOverlayRenderer 课程,请在 cmets 中询问。我的渲染器只是使用 `context.draw

  • 绘制图像

提前致谢

【问题讨论】:

  • 问题在于,您唯一可以制作动画的是视图(或图层),而您没有其中之一。
  • 谢谢。所以@马特。处理这个问题的最佳方法是什么?
  • 好吧,我真的不认为你的飞机一开始就适合作为覆盖物。叠加层是描绘地球表面某些区域的图形。注释是与坐标相关联的图形。我认为你会更喜欢一个注释,它的绘图大小会随着地图的缩放而变化,例如这里讨论的:stackoverflow.com/questions/8286711/…

标签: ios swift animation mkmapview mkoverlay


【解决方案1】:

我求助于从 CLLocationsCoords 计算“UIKit”坐标,添加一个 std UIImageView 所以我移动动画.. 糟糕的解决方案,但非常有效。 :(

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    相关资源
    最近更新 更多