我使用谷歌地图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()
}
答案 0 :(得分:5)
我也在尝试类似的事情(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"];
}
在我的情况下,我必须为整个路线制作动画。在您的情况下,您只需要为更新的部分设置动画。