(未显示1个用户的11个中间版本) | |||
第1行: | 第1行: | ||
− | <div id="examples_center"> | + | <div id="examples_center"><!-- 页面标题 --><div class="title-hd">平滑移动</div><!-- 标题下子标题及简介,可有多个 --><div class="title-mi">功能场景</div><div class="wrap-mi">出行、运动健康类app中动态展示小车、用户的轨迹变化。</div><!-- --></div><div class="demos_wrap"><div class="header"><div class="andiord-header active">Android</div><div class="ios-header">iOS</div></div><div class="andiord-wrap wrap"><div class="wrap-wp"><div class="wrap_left"><div class="left-video"><div class="img-bg-and"></div><div class="phone-android"><div class="video-wrap infor"><div class="video-control"></div><!-- 安卓视频位置 (不写协议)--><pre>//mapopen-website-wiki.bj.bcebos.com/demos/AndroidVideos/Android平滑移动.mp4</pre> |
− | + | </div><div class="video-icon-and"><div class="play play-and"></div><div class="stop stop-and"></div></div></div><div class="progressWrap-hide-and"></div><div id="videoControls-and"><div id="progressWrap-and"><div id="playProgress-and"></div></div></div></div><div class="left-qrCode"><!-- 安卓二维码位置 --><div class="qrCode-and">1590746640|//mapopen-website-wiki.bj.bcebos.com/demos/newqrcodes/平滑移动.png</div><div>扫码体验</div></div></div><div class="wrap_right"><div class="wrap_right_nav"><div class="wrap_right_nav_l"><div>使用产品</div><div><!-- 安卓使用产品下选项,可有多个 --><div class="documentA">Android地图SDK|/index.php?title=androidsdk</div></div></div><div class="wrap_right_nav_r">下载源码</div><!-- 安卓源码地址 --><div class="github-url-and">//mapopen-website-wiki.bj.bcebos.com/demos/BaiduMapSDKExample.zip</div></div><div class="wrap_table"><div class="table_nav"><div class="left-icon"></div><div>核心接口</div></div><div class="table_wrap"><div class="table_wrap_title"><div>类</div><div>接口</div><div>描述</div></div><!-- 表格接口,可有多个 --><div class="table_wrap_con"><div class="table_wrap_w"><div><div class="table-wrap-className">PolylineOptions</div></div><div><div>points(List<LatLng> points)</div></div><div><div>设置Polyline坐标点列表</div></div></div><!-- --><div class="table_wrap_w"><div><div class="table-wrap-className">PolylineOptions</div></div><div><div>width(int width)</div></div><div><div>设置Polyline宽度</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">PolylineOptions</div></div><div><div>customTexture(BitmapDescriptor customTexture)</div></div><div><div>设置自定义纹理</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">PolylineOptions</div></div><div><div>dottedLineType(PolylineDottedLineType polylineDottedLineType)</div></div><div><div>设置Polyline的虚线类型</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">BaiduMap</div></div><div><div>addOverlay(OverlayOptions options)</div></div><div><div>向地图添加一个overlay</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">MarkerOptions</div></div><div><div>flat(boolean flat)</div></div><div><div>设置 marker设置是否平贴地图</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">MarkerOptions</div></div><div><div>anchor(float anchorX, float anchorY)</div></div><div><div>设置 marker 覆盖物的锚点比例,默认(0.5f, 1.0f)水平居中,垂直下对齐</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">MarkerOptions</div></div><div><div>icon(BitmapDescriptor icon)</div></div><div><div>设置Marker图标</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">MarkerOptions</div></div><div><div>position(LatLng position)</div></div><div><div>设置 marker 覆盖物的位置坐标</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">MarkerOptions</div></div><div><div>rotate(float rotate)</div></div><div><div>设置 marker 覆盖物旋转角度,逆时针</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">Marker</div></div><div><div>setPosition(LatLng position)</div></div><div><div>设置Marker位置坐标</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">BitmapDescriptorFactory</div></div><div><div>fromResource(int resourceId)</div></div><div><div>根据资源Id创建不适配设备像素密度的bitmap描述信息</div></div></div></div></div></div></div></div><div class="pre-nav"><div class="left-icon"></div><div>核心代码</div></div><div class="pre-nav"><div>1.绘制轨迹</div></div><div class="pre-wrap"><div class="pre-wrap-nav"><div>JAVA</div></div><!-- 安卓示例代码位置 --><div class="pre-wrap-w pre-wrap-w-and"><pre class="prettyprint prettyprinted pre-and">private void drawPolyLine() { | |
− | + | ||
− | + | ||
− | + | ||
− | </div> | + | |
− | <div class="demos_wrap"> | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | private void drawPolyLine() { | + | |
List list = Arrays.asList(latlngs); | List list = Arrays.asList(latlngs); | ||
List<LatLng> polylines = new ArrayList<>(); | List<LatLng> polylines = new ArrayList<>(); | ||
第251行: | 第23行: | ||
</pre> | </pre> | ||
− | + | <div class="pre-btn"><div class="pre-btn-copy-and">复制</div><div class="pre-btn-cb-and">深色</div></div><div class="success">复制成功</div></div></div><div class="pre-nav"><div>2.移动小车</div></div><div class="pre-wrap"><div class="pre-wrap-nav"><div>JAVA</div></div><!-- 安卓示例代码位置 --><div class="pre-wrap-w pre-wrap-w-and"><pre class="prettyprint prettyprinted pre-and">/** | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | /** | + | |
* 循环进行移动逻辑 | * 循环进行移动逻辑 | ||
*/ | */ | ||
第295行: | 第50行: | ||
double intercept = getInterception(slope, startPoint); | double intercept = getInterception(slope, startPoint); | ||
double xMoveDistance = | double xMoveDistance = | ||
− | isXReverse ? getXMoveDistance(slope) : -1 * getXMoveDistance(slope); | + | isXReverse? getXMoveDistance(slope): -1 * getXMoveDistance(slope); |
double yMoveDistance = | double yMoveDistance = | ||
− | isYReverse ? getYMoveDistance(slope) : -1 * getYMoveDistance(slope); | + | isYReverse? getYMoveDistance(slope): -1 * getYMoveDistance(slope); |
for (double j = startPoint.latitude, k = startPoint.longitude; | for (double j = startPoint.latitude, k = startPoint.longitude; | ||
− | + | !((j > endPoint.latitude) | |
^ isYReverse) | ^ isYReverse) | ||
− | && !((k > endPoint.longitude) | + | &&!((k > endPoint.longitude) |
^ isXReverse); ) { | ^ isXReverse); ) { | ||
LatLng latLng = null; | LatLng latLng = null; | ||
第346行: | 第101行: | ||
</pre> | </pre> | ||
− | + | <div class="pre-btn"><div class="pre-btn-copy-and">复制</div><div class="pre-btn-cb-and">深色</div></div><div class="success">复制成功</div></div></div></div><div class="ios-wrap wrap" style="display: none;"><div class="wrap-wp"><div class="wrap_left"><div class="left-video"><div class="img-bg-ios-sh"></div><div class="phone-ios"><div class="video-wrap infor"><div class="video-control"></div><!-- ios视频位置 --><pre>//mapopen-website-wiki.bj.bcebos.com/demos/iosVideos/iOS平滑移动.mov</pre> | |
− | + | </div><div class="video-icon-ios"><div class="play play-ios"></div><div class="stop stop-ios"></div></div></div><div class="progressWrap-hide-ios"></div><div id="videoControls-ios"><div id="progressWrap-ios"><div id="playProgress-ios"></div></div></div></div><div class="left-qrCode iosqrCode"><!-- ios二维码位置 --><div class="qrCode-ios">1590746640|//mapopen-website-wiki.bj.bcebos.com/demos/ioscodes/平滑移动@1xios.png</div><div>扫码体验</div></div></div><div class="wrap_right"><div class="wrap_right_nav"><div class="wrap_right_nav_l"><div>使用产品</div><div><div class="documentA">iOS地图SDK|/index.php?title=iossdk</div><!-- --></div></div><div class="wrap_right_nav_r">下载源码</div><!-- IOS源码地址 --><div class="github-url-ios">//mapopen-website-wiki.bj.bcebos.com/BaiduMapSDKDemo.zip</div></div><div class="wrap_table"><div class="table_nav"><div class="left-icon"></div><div>核心接口</div></div><div class="table_wrap"><div class="table_wrap_title"><div>类</div><div>接口</div><div>描述</div></div><!-- ios表格接口内容,可有多个 --><!-- 表格接口,可有多个 --><div class="table_wrap_con"><div class="table_wrap_w"><div><div class="table-wrap-className">BMKPointAnnotation</div></div><div><div>- (instancetype)init</div></div><div><div>创建点标记实例</div></div></div><!-- --><div class="table_wrap_w"><div><div class="table-wrap-className">BMKPointAnnotation</div></div><div><div>CLLocationCoordinate2D coordinate;</div></div><div><div>设置点标记经纬度坐标</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">BMKPointAnnotation</div></div><div><div>NSString *title;</div></div><div><div>设置显示的标题</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">BMKPinAnnotationView</div></div><div><div>- (id)initWithAnnotation:(id <BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;</div></div><div><div>初始化并返回一个BMKPinAnnotationView实例</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">BMKPolygon</div></div><div><div>+ (BMKPolygon *)polygonWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUInteger)count;</div></div><div><div>根据多个点生成多边形</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">BMKPolygonView</div></div><div><div>- (id)initWithOverlay:(id <BMKOverlay>)overlay;</div></div><div><div>初始化并返回一个BMKPolygonView实例,模拟运动轨迹</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">BMKMapView</div></div><div><div>- (void)addAnnotation:(id <BMKAnnotation>)annotation;</div></div><div><div>向地图窗口添加点标记</div></div></div><div class="table_wrap_w"><div><div class="table-wrap-className">UIView</div></div><div><div>+ (void)animateWithDuration:(NSTimeInterval)durationdelay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOLfinished))completion API_AVAILABLE(ios(4.0));</div></div><div><div>基于UIView动画完成连续平滑移动</div></div></div></div></div></div></div></div><div class="pre-nav"><div class="left-icon"></div><div>核心代码</div></div><div class="pre-nav"><div>1.动画相关属性设置</div></div><div class="pre-wrap"><div class="pre-wrap-nav"><div>Objective-C</div></div><div class="pre-wrap-w pre-wrap-w-ios code_container"><!-- ios示例代码位置 --><pre class="prettyprint prettyprinted pre-ios">#pragma mark - 动画相关 | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | #pragma mark - 动画相关 | + | |
- (void)startAnimation { | - (void)startAnimation { | ||
double speed = 80.0 / 3.6; | double speed = 80.0 / 3.6; | ||
第524行: | 第109行: | ||
[UIView animateWithDuration:node.distance / speed | [UIView animateWithDuration:node.distance / speed | ||
delay:0.0 options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction animations:^(void) { | delay:0.0 options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction animations:^(void) { | ||
− | [self.annotation setCoordinate:((BMKSportNode *)self.sportNodes[(self.currentIndex + 1) % self.sportNodeNum]).coordinate]; | + | [self.annotation setCoordinate:((BMKSportNode *)self.sportNodes[(self.currentIndex + 1)% self.sportNodeNum]).coordinate]; |
} completion:^(BOOL finished) { | } completion:^(BOOL finished) { | ||
if (!self.stopAnimation) { | if (!self.stopAnimation) { | ||
self.currentIndex ++; | self.currentIndex ++; | ||
− | self.currentIndex %= self.sportNodes.count; | + | self.currentIndex%= self.sportNodes.count; |
__weak id weakSelf = self; | __weak id weakSelf = self; | ||
[[NSOperationQueue mainQueue] addOperationWithBlock:^{ | [[NSOperationQueue mainQueue] addOperationWithBlock:^{ | ||
第537行: | 第122行: | ||
} | } | ||
</pre> | </pre> | ||
− | + | <div class="pre-btn"><div class="pre-btn-copy-ios">复制</div><div class="pre-btn-cb-ios">深色</div></div><div class="success">复制成功</div></div></div><div class="pre-nav"><div>2.小车运动轨迹点方向、距离的计算</div></div><div class="pre-wrap"><div class="pre-wrap-nav"><div>Objective-C</div></div><div class="pre-wrap-w pre-wrap-w-ios code_container"><!-- ios示例代码位置 --><pre class="prettyprint prettyprinted pre-ios">#pragma mark - SportPath | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | #pragma mark - SportPath | + | |
- (void)fetchSportNodes { | - (void)fetchSportNodes { | ||
self.sumDistance = 0; | self.sumDistance = 0; | ||
第565行: | 第132行: | ||
NSDictionary *current = array[i]; | NSDictionary *current = array[i]; | ||
CLLocationCoordinate2D startCoor = CLLocationCoordinate2DMake([current[@"lat"] doubleValue], [current[@"lon"] doubleValue]); | CLLocationCoordinate2D startCoor = CLLocationCoordinate2DMake([current[@"lat"] doubleValue], [current[@"lon"] doubleValue]); | ||
− | NSDictionary *next = array[(i + 1) % array.count]; | + | NSDictionary *next = array[(i + 1)% array.count]; |
CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake([next[@"lat"] doubleValue], [next[@"lon"] doubleValue]); | CLLocationCoordinate2D endCoor = CLLocationCoordinate2DMake([next[@"lat"] doubleValue], [next[@"lon"] doubleValue]); | ||
CLLocation *begin = [[CLLocation alloc] initWithLatitude:startCoor.latitude longitude:startCoor.longitude]; | CLLocation *begin = [[CLLocation alloc] initWithLatitude:startCoor.latitude longitude:startCoor.longitude]; | ||
第583行: | 第150行: | ||
</pre> | </pre> | ||
− | + | <div class="pre-btn"><div class="pre-btn-copy-ios">复制</div><div class="pre-btn-cb-ios">深色</div></div><div class="success">复制成功</div></div></div></div></div> | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | </div> | + |
2022年2月25日 (五) 17:25的最后版本
平滑移动
功能场景
出行、运动健康类app中动态展示小车、用户的轨迹变化。
Android
iOS
//mapopen-website-wiki.bj.bcebos.com/demos/AndroidVideos/Android平滑移动.mp4
1590746640|//mapopen-website-wiki.bj.bcebos.com/demos/newqrcodes/平滑移动.png
扫码体验
使用产品
Android地图SDK|/index.php?title=androidsdk
下载源码
//mapopen-website-wiki.bj.bcebos.com/demos/BaiduMapSDKExample.zip
核心接口
类
接口
描述
PolylineOptions
points(List<LatLng> points)
设置Polyline坐标点列表
PolylineOptions
width(int width)
设置Polyline宽度
PolylineOptions
customTexture(BitmapDescriptor customTexture)
设置自定义纹理
PolylineOptions
dottedLineType(PolylineDottedLineType polylineDottedLineType)
设置Polyline的虚线类型
BaiduMap
addOverlay(OverlayOptions options)
向地图添加一个overlay
MarkerOptions
flat(boolean flat)
设置 marker设置是否平贴地图
MarkerOptions
anchor(float anchorX, float anchorY)
设置 marker 覆盖物的锚点比例,默认(0.5f, 1.0f)水平居中,垂直下对齐
MarkerOptions
icon(BitmapDescriptor icon)
设置Marker图标
MarkerOptions
position(LatLng position)
设置 marker 覆盖物的位置坐标
MarkerOptions
rotate(float rotate)
设置 marker 覆盖物旋转角度,逆时针
Marker
setPosition(LatLng position)
设置Marker位置坐标
BitmapDescriptorFactory
fromResource(int resourceId)
根据资源Id创建不适配设备像素密度的bitmap描述信息
核心代码
1.绘制轨迹
JAVA
private void drawPolyLine() { List list = Arrays.asList(latlngs); List<LatLng> polylines = new ArrayList<>(); polylines.addAll(list); polylines.add(latlngs[0]); // 绘制纹理PolyLine PolylineOptions polylineOptions = new PolylineOptions().points(polylines).width(25).customTexture(mGreenTexture) .dottedLine(true); mPolyline = (Polyline) mBaiduMap.addOverlay(polylineOptions); // 添加小车marker OverlayOptions markerOptions = new MarkerOptions() .flat(true) .anchor(0.5f, 0.5f) .icon(mBitmapCar). position(polylines.get(0)) .rotate((float) getAngle(0)); mMoveMarker = (Marker) mBaiduMap.addOverlay(markerOptions); }
复制
深色
复制成功
2.移动小车
JAVA
/** * 循环进行移动逻辑 */ public void moveLooper() { new Thread() { public void run() { while (true) { for (int i = 0; i < latlngs.length - 1; i++) { final LatLng startPoint = latlngs[i]; final LatLng endPoint = latlngs[i + 1]; mMoveMarker.setPosition(startPoint); mHandler.post(new Runnable() { @Override public void run() { // refresh marker's rotate if (mMapView == null) { return; } mMoveMarker.setRotate((float) getAngle(startPoint, endPoint)); } }); double slope = getSlope(startPoint, endPoint); // 是不是正向的标示 boolean isYReverse = (startPoint.latitude > endPoint.latitude); boolean isXReverse = (startPoint.longitude > endPoint.longitude); double intercept = getInterception(slope, startPoint); double xMoveDistance = isXReverse? getXMoveDistance(slope): -1 * getXMoveDistance(slope); double yMoveDistance = isYReverse? getYMoveDistance(slope): -1 * getYMoveDistance(slope); for (double j = startPoint.latitude, k = startPoint.longitude; !((j > endPoint.latitude) ^ isYReverse) &&!((k > endPoint.longitude) ^ isXReverse); ) { LatLng latLng = null; if (slope == Double.MAX_VALUE) { latLng = new LatLng(j, k); j = j - yMoveDistance; } else if (slope == 0.0) { latLng = new LatLng(j, k - xMoveDistance); k = k - xMoveDistance; } else { latLng = new LatLng(j, (j - intercept) / slope); j = j - yMoveDistance; } final LatLng finalLatLng = latLng; if (finalLatLng.latitude == 0 && finalLatLng.longitude == 0) { continue; } mHandler.post(new Runnable() { @Override public void run() { if (mMapView == null) { return; } mMoveMarker.setPosition(finalLatLng); // 设置 Marker 覆盖物的位置坐标,并同步更新与Marker关联的InfoWindow的位置坐标. mMoveMarker.setPositionWithInfoWindow(finalLatLng); } }); try { Thread.sleep(TIME_INTERVAL); } catch (InterruptedException e) { e.printStackTrace(); } } } } } }.start(); }
复制
深色
复制成功