java 轨迹平滑算法_平滑移动-SDK 示例 | 高德地图API

本文介绍了如何在高德地图API中使用Java实现轨迹平滑移动的效果,涉及MovingAnnotationView和CACoordLayer的使用,以及如何添加动画使车辆在地图上平滑移动。
摘要由CSDN通过智能技术生成

使用场景

出行、运动等类别的app中常常会需要展示车辆或用户的行程轨迹、实时移动轨迹等数据,相应效果需要车辆在地图上平滑移动。该demo展示了如何实现该效果。

用到产品

核心类/接口

类接口说明版本

MAMapview- (void)addOverlay:(id )overlay;添加轨迹线V4.0.0起

MAMapview- (void)addAnnotation:(id )annotation;添加汽车V4.0.0起

MovingAnnotationView- (void)addTrackingAnimationForPoints:(NSArray *)points duration:(CFTimeInterval)duration;添加动画----

CACoordLayer- (void)display;显示当前帧----

核心难点

1、MovingAnnotationView 自定义annotationview,其layer是自定义的CACoordLayer

/**

动画AnnotationView,只试用于高德3D地图SDK。

*/

@interface MovingAnnotationView : MAAnnotationView

/*!

@brief 添加动画

@param points 轨迹点串,每个轨迹点为TracingPoint类型

@param duration 动画时长,包括从上一个动画的终止点过渡到新增动画起始点的时间

*/

- (void)addTrackingAnimationForPoints:(NSArray *)points duration:(CFTimeInterval)duration;

@end

+ (Class)layerClass

{

return [CACoordLayer class];

}

func initRoute() {

let count: Int = s_coords.count

self.fullTraceLine = MAPolyline(coordinates: &s_coords, count: UInt(count))

self.mapView.add(self.fullTraceLine)

var routeAnno = [Any]()

for i in 0..

let a = MAPointAnnotation()

a.coordinate = s_coords[i]

a.title = "route"

routeAnno.append(a)

}

self.mapView.addAnnotations(routeAnno)

self.mapView.showAnnotations(routeAnno, animated: false)

self.car1 = MAAnimatedAnnotation()

self.car1.title = "Car1"

self.mapView.addAnnotation(self.car1)

weak var weakSelf = self

self.car2 = CustomMovingAnnotation()

self.car2.stepCallback = {() -> Void in

weakSelf?.updatePassedTrace()

}

self.car2.title = "Car2"

self.mapView.addAnnotation(self.car2)

self.car1.coordinate = s_coords[0]

self.car2.coordinate = s_coords[0]

}

2、CACoordLayer,每次显示时根据mapPoint计算屏幕坐标

- (void)display

{

CACoordLayer * layer = [self presentationLayer];

MAMapPoint mappoint = MAMapPointMake(layer.mapx, layer.mapy);

CGPoint center = [self.mapView pointForMapPoint:mappoint];

center.x += self.centerOffset.x;

center.y += self.centerOffset.y;

self.position = center;

}

func mov() {

let speed_car1: Double = 120.0 / 3.6

//80 km/h

let count: Int = s_coords.count

self.car1.coordinate = s_coords[0]

let duration = self.sumDistance / speed_car1;

self.car1.addMoveAnimation(withKeyCoordinates: &s_coords, count: UInt(count), withDuration: CGFloat(duration), withName: nil, completeCallback: {(_ isFinished: Bool) -> Void in

})

//小车2走过的轨迹置灰色, 采用添加多个动画方法

let speed_car2: Double = 100.0 / 3.6

//60 km/h

weak var weakSelf = self

self.car2.coordinate = s_coords[0]

self.passedTraceCoordIndex = 0

for i in 1..

let num = self.distanceArray[i - 1]

let tempDuration = num / speed_car2

self.car2.addMoveAnimation(withKeyCoordinates: &(s_coords[i]), count: 1, withDuration: CGFloat(tempDuration), withName: nil, completeCallback: {(_ isFinished: Bool) -> Void in

weakSelf?.passedTraceCoordIndex = i

})

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值