您的位置:首页 > 移动开发 > Android开发

android 实现计算百度地图两点之间旋转角度,多点有向连接

2018-03-28 16:23 726 查看
地图文档:http://wiki.lbsyun.baidu.com/cms/androidsdk/doc/v3_4_0/
private int MUL = 100_0000;
private List<Overlay> overlayList = new ArrayList<>();
private LatLng latlngLarger(LatLng latLng) {
return new LatLng(latLng.latitude * MUL, latLng.longitude * MUL);
}

private Float rotation(LatLng bigStart, LatLng bigEnd) {
bigStart = latlngLarger(bigStart);
bigEnd = latlngLarger(bigEnd);
if (bigEnd.longitude != bigStart.longitude) {
double tan = (bigEnd.latitude - bigStart.latitude) / (bigEnd.longitude - bigStart.longitude);
double atan = Math.atan(tan);
double deg = atan * 360 / (2 * Math.PI);
if (bigEnd.longitude < bigStart.longitude) {
deg = -deg + 90 + 90;
} else {
deg = -deg;
}
return (float) -deg;
} else {
double disy = bigEnd.latitude - bigStart.latitude;
int bias = 1;
if (disy > 0) {
bias = -1;
}
return -bias * 90f;
}
}
调用方式:
BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.ic_start_location);
float rtateF = rotation(desLatLng, desLatLngNext);
OverlayOptions option = new MarkerOptions().position(desLatLng).icon(bitmap).anchor(0.5f, 0.5f).rotate(rtateF);

//在地图上添加Marker,并显示
Overlay overlay = mBaiduMap.addOverlay(option);
Bundle bundle = new Bundle();
bundle.putInt("index", i);
overlay.setExtraInfo(bundle);
overlayList.add(overlay);

附带地图弹窗及点击事件:
Button button = new Button(this);
button.setText(carLocationInfo.getCarPosition().get(marker.getExtraInfo().getInt("index")).getGtm());
button.setBackgroundResource(R.drawable.shape_rect_cor_blue_white);
//定义用于显示该InfoWindow的坐标点
LatLng pt = marker.getPosition();
//创建InfoWindow , 传入 view, 地理坐标, y 轴偏移量
InfoWindow mInfoWindow = new InfoWindow(button, pt, -47);
//显示InfoWindow
mBaiduMap.showInfoWindow(mInfoWindow);
button.setOnClickListener(view -> {
mBaiduMap.hideInfoWindow();
});


private List<Overlay> overlayList = new ArrayList<>();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: