如何使MKAnotnotations在地图上平滑移动(调整其坐标)?

时间:2018-08-30 19:51:02

标签: ios json swift gps mkannotation

我是Swift的新手开发人员,并且正在构建一个可在地图上跟踪城市公交车位置的应用。

我的问题是,我希望MKAnnotations在GPS位置之间平滑移动(从旧位置到新JSON中的位置),而不是被删除并重新出现。

到目前为止,我的应用程序正在检索JSON,并将其解码为遵循MKAnnotation协议的对象数组。

然后我用这种方法将它们全部显示在地图上:

func updateUI(json: MyModelClass) {
    mapView.removeAnnotations(mapView.annotations)
    mapView.addAnnotations(json.busArray)
}

JSON每10秒下载一次,并触发updateUI方法,删除现有的MKA注释并添加新的注释。

对于任何建议,我将不胜感激。

如果需要的话,这里是模型:

struct MyModelClass: Codable {
    let busArray: [Bus]

    enum CodingKeys: String, CodingKey {
        case busArray = "result"
    }

    init(busArray: [Bus]) {
        self.busArray = busArray
    }
}

class Bus: NSObject, Codable, MKAnnotation {
    let latitude, longitude: Double
    let time, title, brigade: String?
    var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 52.22977, longitude: 21.01178)

    enum CodingKeys: String, CodingKey {
        case latitude = "Lat"
        case longitude = "Lon"
        case time = "Time"
        case title = "Lines"
        case brigade = "Brigade"
    }

    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)

        self.latitude = try container.decode(Double.self, forKey: .latitude)
        self.longitude = try container.decode(Double.self, forKey: .longitude)
        self.title = try container.decode(String.self, forKey: .title)
        self.time = try container.decode(String.self, forKey: .time)
        self.brigade = try container.decode(String.self, forKey: .brigade)
        self.coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(latitude), longitude: CLLocationDegrees(longitude))

3 个答案:

答案 0 :(得分:0)

我将busID字段添加到Bus类。要将总线添加到地图,我将创建一个专门添加新总线的方法。然后使用updateUI方法,应使用for循环(或等效方法)来匹配busID并修改其新位置。

我希望这可以引导您朝正确的方向前进。

答案 1 :(得分:0)

像这样吗?

enter image description here

MKAnnotationView继承自UIView。

我这样做是通过将注解视图作为注解添加到MapView

相反,我做了类似的事情

mapView.addSubview(annotaionView)

此后,请使用iOS的一种方法制作UIViews的动画:

最简单的方法是使用UIView.animate{ }

在上面的示例中,我使用了UIKit dynamics和SnapBehaviour,请参阅https://www.raywenderlich.com/2326-uikit-dynamics-tutorial-getting-started

您需要做的一件事就是将地图坐标转换为UIView坐标:

 var snapTo = mapView.convert(cdPOI.coordinate, toPointTo: mapView)

动画制作完成后,我将注释作为子视图删除,并执行类似mapView.addAnnotation(annotation)

的操作

答案 2 :(得分:0)

这是我根据OverD的答案编写的代码段。 subtitle的值与busID相同。

DispatchQueue.global().async {

                for old in self.mapView.annotations {
                    for new in model.busArray {
                        if new.busID == old.subtitle {
                            if let annotation = old as? Bus {
                                DispatchQueue.main.async {                          
                                    annotation.coordinate = new.coordinate
                                }
                            }
                        }
                    }
                }

            }

编辑-重构并使注解视图从旧坐标平稳地移动到新坐标。

func matchIDs(mapView: MKMapView, annotations: [MKAnnotation], model: MyModel) {

        DispatchQueue.global().async {
            for annotation in annotations {
                if let filteredBus = model.busArray.filter({ $0.busID == annotation.subtitle }).first {
                    DispatchQueue.main.async {

                        UIView.animate(withDuration: 0.5, animations: {
                            let matchedBus = annotation
                            if let annotationForView = mapView.view(for: matchedBus)?.annotation as? Bus {
                                annotationForView.coordinate = filteredBus.coordinate
                            }
                        })

                    }
                } else {
                    // do nothing
                }
            }
        }
    }

我还在我的MKMapView委托(视图控制器)中实现了一个viewFor annotation方法,该方法为每个注释创建了一个自定义图标。