是否可以使用百度地图在折线上添加箭头?
我找到了自己的答案,并发布了答案,这样可以帮助他人。在我的情况下,我需要在多边形线的末端添加箭头。
我没有发现任何内置的方式来做这与百度SDK,所以我提出了自定义的解决方案。其思想是找到多边形线最后两点之间的航向角,并将箭头位图旋转该角度,然后将该角度定位在多边形线的末端。
解决方案
val points = polyline.points
val p1 = points[points.size -1]
val p2 = points[points.size -2]
// heading angle between points (+180 is for bringing the value in 0..360 range, as heading's default range is -180..180 degree)
val angle = SphericalUtil.computeHeading(p1,p2)+180 // SphericalUtil link -> https://github.com/googlemaps/android-maps-utils
// rotate the bitmap to fit last two points direction
val rotatedArrowBitmap = arrowBitmap.rotate(angle) // .rotate(value) is a kotlin extention, find ful method below
val markerOptions = MarkerOptions().position(LatLng(p1.latitude, p1.longitude))
.icon(BitmapDescriptorFactory.fromBitmap(bitmap))
.anchor(0.5f, 0.5f) // this will fit marker center with the p1 coordinate
.flat(true) // this will make marker rotate with map
map.addOverlay(markerOptions)