gpt4 book ai didi

ios - 我如何在谷歌地图 ios 中为 GMSPolyline 设置动画

转载 作者:技术小花猫 更新时间:2023-10-29 10:38:49 28 4
gpt4 key购买 nike

我使用的是谷歌地图 SDK,我必须在用户移动时绘制多段线,目前它只为标记设置动画,在图钉移动到特定位置之前绘制路径。我必须同时绘制路径和移动图钉。

查看此视频: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()
}

这里还有截图,它现在是如何工作的 Sample Image

最佳答案

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

尝试以多种方式对 GMSPolyline 路径进行动画处理,但未能直接对其进行动画处理。

我怎么找到替代方法来做到这一点。你所要做的就是创建一个带有 BreizerPath 的 CAShapeLayer,将其伪装成 GMSPolyline 并为 strokeEnd 设置动画。动画完成后,显示实际的 GMSPolyline 并移除 CAShapelayer。但是我必须对其进行优化。

为了创建一个 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;
}

下面是动画代码。

-(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"];
}

在我的例子中,我必须为整条路线制作动画。在您的情况下,您只需为更新的部分设置动画。

关于ios - 我如何在谷歌地图 ios 中为 GMSPolyline 设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38221641/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com