我如何在谷歌地图iosanimationGMSPolyline

我正在使用谷歌地图SDK,我必须在用户移动时绘制多段线,目前它只是animation标记,在引脚移动到特定位置之前绘制path。 我必须同时绘制path和移动引脚。

查看此video: https : //www.dropbox.com/s/q5kdjf4iq0337vg/Map_Sample.mov?dl = 0

这是我的代码

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let location = locations.last! userPath.addCoordinate(location.coordinate) //userPath -> GMSMutablePath let polyline = GMSPolyline(path: userPath) polyline.strokeColor = UIColor(red: 0, green: 191/255.0, blue: 1, alpha: 0.8) polyline.strokeWidth = 5 CATransaction.begin() CATransaction.setAnimationDuration(2.0) self.userMarker.position = location.coordinate self.userMarker.rotation = location.course polyline.map = self.googleMapView CATransaction.commit() } 

另外这里的截图是如何工作的 示例图像

我也在尝试类似的东西( https://youtu.be/jej2XTwRQy8 )。

尝试以多种方式对GMSPolylinepath进行animation处理,但无法直接对其进行animation处理。

我怎么能find一个替代scheme来做到这一点。 所有你需要做的就是创build一个带有BreizerPath的CAShapeLayer,假造它看起来像一个GMSPolyline,并为strokeEnd设置animation。 一旦animation完成,显示实际的GMSPolyline并删除CAShapelayer。 不过,我必须优化它。

为了创build一个CAShapeLayer,以及开始的几行,你可以参考下面的代码。

 -(CAShapeLayer *)layerFromGMSMutablePath:(GMSMutablePath *)path{ UIBezierPath *breizerPath = [UIBezierPath bezierPath]; CLLocationCoordinate2D firstCoordinate = [path coordinateAtIndex:0]; [breizerPath moveToPoint:[_mapView.projection pointForCoordinate:firstCoordinate]]; for(int i=1; i<path.count; i++){ CLLocationCoordinate2D coordinate = [path coordinateAtIndex:i]; [breizerPath addLineToPoint:[_mapView.projection pointForCoordinate:coordinate]]; } CAShapeLayer *shapeLayer = [CAShapeLayer layer]; shapeLayer.path = [[breizerPath bezierPathByReversingPath] CGPath]; shapeLayer.strokeColor = [[UIColor whiteColor] CGColor]; shapeLayer.lineWidth = 4.0; shapeLayer.fillColor = [[UIColor clearColor] CGColor]; shapeLayer.lineJoin = kCALineJoinRound; shapeLayer.lineCap = kCALineCapRound; shapeLayer.cornerRadius = 5; return shapeLayer; } 

以下是animation的代码。

 -(void)animatePath:(CAShapeLayer *)layer{ CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; pathAnimation.duration = 3; pathAnimation.delegate = self; [pathAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f]; pathAnimation.toValue = [NSNumber numberWithFloat:1.0f]; [layer addAnimation:pathAnimation forKey:@"strokeEnd"]; } 

如何在我的情况下,我不得不animation整个路线。 在你的情况下,你只需要animation更新的部分。