百度地图开发的一些问题记录

1.规划路线绘制(使用纹理图片做路径底图)

    List<BitmapDescriptor> textureList = new ArrayList<>();
    List<Integer> integerList = new ArrayList<>();
	//引入纹理图片,纹理图片长宽要为2的次方,如16、32、64、128
	Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_raod_arrow);
	//如果Android引入纹理图片时,改变了图片的大小,会使路径变成断断续续的断线,这时候需要重新设置图片的大小
	Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 32, 128, true);
	bitmap.recycle();
	BitmapDescriptor texture = BitmapDescriptorFactory.fromBitmap(newBitmap);
    textureList.add(texture);
	integerList.add(0);
    OverlayOptions mOverlayOptions = new PolylineOptions()
                //路径轨迹的宽度
                .width(25)
                //路径轨迹点List<LatLng>
                .points(wayPoints)
                .dottedLine(true)
                .dottedLineType(PolylineDottedLineType.DOTTED_LINE_CIRCLE)
                .focus(false)
                //纹理图片序号,这个要放前面
                .textureIndex(integerList)
                //纹理图片资源,这个要放后面
                .customTextureList(textureList);
    //添加到百度地图
	baiduMap.addOverlay(mOverlayOptions);

大概效果图:
在这里插入图片描述

2.计算点集的中心点

    private void calCenter() {
        double xMax = wayPoints.get(0).latitude;
        double yMax = wayPoints.get(0).longitude;
        double xMin = wayPoints.get(0).latitude;
        double yMin = wayPoints.get(0).longitude;
        for (LatLng latLng : wayPoints) {
            xMax = Math.max(latLng.latitude, xMax);
            yMax = Math.max(latLng.longitude, yMax);
            xMin = Math.min(latLng.latitude, xMin);
            yMin = Math.min(latLng.longitude, yMin);
        }
        centerPoint = new LatLng((xMax + xMin) / 2, (yMax + yMin) / 2);
    }

3.定位点往下偏移一定距离(适用于导航时车子偏下,实现车子移动时,车子位于地图中心点下方不动,但是地图可以实时偏转和移动的驾驶效果)

    //偏移的度数
	double R = 0.0015f;
	//地图的偏转角度,正北:0,正东:90,正南:180,正西:270
	double direction;
    /**
     * 偏移一定距离的纬度
     *
     * @return 偏移后的纬度
     */
    private double getOffsetLat() {
        double dx;
        if (direction >= 0 && direction < 90) {
            dx = R * Math.cos(direction / 180 * Math.PI);
        } else if (direction >= 90 && direction < 180) {
            dx = -R * Math.cos((180 - direction) / 180 * Math.PI);
        } else if (direction >= 180 && direction < 270) {
            dx = -R * Math.cos((direction - 180) / 180 * Math.PI);
        } else {
            dx = R * Math.cos((360 - direction) / 180 * Math.PI);
        }
        return dx;
    }

    /**
     * 偏移一定距离的经度
     *
     * @return 偏移后的经度
     */
    private double getOffsetLng() {
        double dy;
        if (direction >= 0 && direction < 90) {
            dy = R * Math.sin(direction / 180 * Math.PI);
        } else if (direction >= 90 && direction < 180) {
            dy = R * Math.sin((180 - direction) / 180 * Math.PI);
        } else if (direction >= 180 && direction < 270) {
            dy = -R * Math.sin((direction - 180) / 180 * Math.PI);
        } else {
            dy = -R * Math.sin((360 - direction) / 180 * Math.PI);
        }
        return dy;
    }
	//地图中心点的纬度偏移
    double dx = getOffsetLat(direction);
    //地图中心点的经度偏移
    double dy = getOffsetLng(direction);
    //原中心点(设置为车子位置)
    LatLng target = new LatLng(lat, lng)
    //新中心点(设置为地图中心)
    LatLng target = new LatLng(lat + dx, lng + dy)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值