【问题标题】:Swift - Movement of GMSMarker along array of CLCoordinates from GMSPath (Google Maps SDK for iOS)Swift - GMSMarker 沿 GMSPath 的 CLCoordinate 数组移动(适用于 iOS 的 Google Maps SDK)
【发布时间】:2018-10-12 05:00:18
【问题描述】:

所有在谷歌地图中在坐标之间动画标记移动的尝试都指向在 Swift 中使用以下 sn-p 代码:

CATransaction.begin()
CATransaction.setAnimationDuration(duration)
marker.position = coordindates
CATransaction.commit()

例如,这里是投票最多的 SO 帖子: How to smoothly move GMSMarker along coordinates in Objective c

当在起点和终点坐标对之间进行动画处理时,这可以正常工作。但是,我正在寻找从 GMSPath 中的起始坐标到结束坐标的动画。当遍历路径中的点时,唯一显示的动画在最后两个坐标之间。标记仅出现在倒数第二个点并动画到最后一个点。

这是我的 ViewController 代码。它正在接收一段路由作为编码路径(为了测试,第一个编码路径:“ika~Exi|vN|AaDzAyCTc@N[lBeEvB_ExBkExBmEjBwDXo@”)。

代码正在遍历 GMSPath 对象内所有存储的坐标,并尝试使用上面发布的 sn-p 进行动画处理。如前所述,它只显示最后两点之间的动画。

我已尝试将所有代码集放在 ViewDidLoad、ViewDidAppear 和 ViewWillAppear 中。 ViewDidLoad 将缩放级别保持在洲际。 ViewDidAppear 和 ViewWillAppear 适当地进行了放大,并导致了本文中提到的动画问题。该代码目前在 ViewDidAppear 和 ViewWillAppear 之间进行拆分,但如果单独放置在任一方法中,其作用相同。

import UIKit
import GoogleMaps
import CoreLocation

class MapVC:UIViewController {

    var mapView:GMSMapView?

    var polyline:GMSPolyline?

    var path:GMSPath?

    var encodedPath:String? = nil

    var marker:GMSMarker?

    override func viewDidLoad() {
        super.viewDidLoad()


        setupMap()

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if encodedPath != nil {

            self.path = GMSPath(fromEncodedPath: encodedPath!)

            self.polyline = GMSPolyline(path: path)
            self.polyline!.map = self.mapView!

            let bounds:GMSCoordinateBounds = GMSCoordinateBounds(path: path!)

            let update = GMSCameraUpdate.fit(bounds, withPadding: 10.0)
            self.mapView!.animate(with: update)



        } else {

            print("nil path")

        }
        let a=2

    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        var index:UInt = 0

        let count:UInt = self.path!.count()

        if count > 0 {

            marker = GMSMarker(position: self.path!.coordinate(at:index))
            marker!.map = self.mapView

            index += 1

            while index < count {

                CATransaction.begin()
                CATransaction.setAnimationDuration(30)
                self.marker!.position = self.path!.coordinate(at:index)
                CATransaction.commit()
                index += 1

            }

        }

    }




    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func setupMap() {

        let camera = GMSCameraPosition.camera(withLatitude: 36.5, longitude: -82.5, zoom: 16)
        mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

        self.view = mapView

    }

}

【问题讨论】:

    标签: ios swift google-maps gmsmapview catransaction


    【解决方案1】:

    找到使用计时器的解决方案:https://github.com/antonyraphel/ARCarMovement/blob/master/ARCarMovementSwift/ARCarMovementSwift/ViewController.swift

    上面显示的视图控制器的更新代码:

        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
    
            marker = GMSMarker(position: self.path!.coordinate(at:self.index))
            marker!.map = self.mapView
    
            timer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: 
                    #selector(MapVC.timerTriggered), userInfo: nil, repeats: true)
    
    
        }
    
    
        @objc func timerTriggered() {
    
            if self.index < self.path!.count() {
    
                CATransaction.begin()
                CATransaction.setAnimationDuration(1.9)
                self.marker!.position = self.path!.coordinate(at:index)
                CATransaction.commit()
                self.index += 1
    
            } else {
    
                timer.invalidate()
                timer = nil
    
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-20
      相关资源
      最近更新 更多