浏览器版本低!无法浏览完整内容,建议升级或更换浏览器。

文档全面上新

更科技的视觉体验,更高效的页面结构,快前往体验吧!

体验新版
平滑移动
功能场景
出行、运动健康类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();
}
               
                
复制
深色
复制成功
  • 文档根本没法用

  • 文档水平很差

  • 文档水平一般

  • 文档不错

  • 文档写的很好

如发现文档错误,或对此文档有更好的建议,请在下方反馈。问题咨询请前往反馈平台提交工单咨询。

提交反馈

拖动标注工具

添加矩形标注

添加箭头标注

完成

取消