How to get animated polyline route in GMSMapView, so that it move along with map when map is moved?

后端 未结 6 1383
独厮守ぢ
独厮守ぢ 2020-12-02 13:37

I have created animated polyline like CAShapeLayer by following code, I have added CAShapeLayer as sublayer to GMSMapiew but, if I move the map the layer won\'t moves. wher

6条回答
  •  情话喂你
    2020-12-02 14:13

    This is an adaptation of the code from Elangovan

    The changes that I did was to remove the var from the class to be just in the function and also removed the #selector that is no longer need in iOS >= 10.

    var timerAnimation: Timer!
    var mapView:GMSMapView?
    
    
    
        func drawRoute(encodedString: String, animated: Bool) {
    
            if let path = GMSMutablePath(fromEncodedPath: encodedString) {
    
                let polyline = GMSPolyline(path: path)
                polyline.strokeWidth = 3.0
                polyline.strokeColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
                polyline.map = Singleton.shared.getMapView()
    
                if(animated){
                    self.animatePolylinePath(path: path)
                }
            }
        }
    
        func animatePolylinePath(path: GMSMutablePath) {
    
            var pos: UInt = 0
            var animationPath = GMSMutablePath()
            let animationPolyline = GMSPolyline()
            self.timerAnimation = Timer.scheduledTimer(withTimeInterval: 0.003, repeats: true) { timer in
    
                pos += 1
                if(pos >= path.count()){
                    pos = 0
                    animationPath = GMSMutablePath()
                    animationPolyline.map = nil
                }
                animationPath.add(path.coordinate(at: pos))
                animationPolyline.path = animationPath
                animationPolyline.strokeColor = UIColor.yellow
                animationPolyline.strokeWidth = 3
                animationPolyline.map = self.mapView
            }
        }
    
    
        func stopAnimatePolylinePath() {
    
            self.timerAnimation.invalidate()
        }
    

提交回复
热议问题