简体   繁体   中英

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM