百度地图开发的一些问题记录
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)