0

是否可以使用百度地图在折线上添加箭头?

4

1 回答 1

0

我找到了自己的答案并发布了答案,以便对其他人有所帮助。就我而言,我需要在折线的末尾添加箭头。

我没有找到任何使用百度 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)
于 2020-08-21T13:01:51.120 回答