Android: Add arrow heads on poly line using Baidu map?

103 views Asked by At

Is it possible to add arrows heads on poly line using Baidu map?

1

There are 1 answers

0
Robert Apikyan On

I found answer my self and posting answer so it can help to others. In my case I need to add arrow at the end of the poly line.

I do not find any built in way to do this with Baidu SDK, so I came up with custom solution. The idea is to find the heading angle between last two points of the poly line, and rotate the arrow bitmap by that angle, and position the angle at the end of poly line.

Solution.

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)