Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polyline with infowindow in android app

In android map application, it shows tool-tip window for multiple route, the same way google map also shows that window. I was wondering whether that is a custom marker or a infoWindow over poly-line.

Does any body knows how to achieve this with android map-v2 integration?

Following image show my expectations to implement in android.

Snapshot from google map

like image 626
NetDemo Avatar asked Nov 03 '16 06:11

NetDemo


1 Answers

2020 Solution (Kotlin)

I wrote this little Kotlin extension function (simply add to your extension functions or anywhere in your code) to do exactly the job needed: It adds an info window half way on the polyline.

fun Polyline.addInfoWindow(map: GoogleMap, title: String, message: String) {
    val pointsOnLine = this.points.size
    val infoLatLng = this.points[(pointsOnLine / 2)]
    val invisibleMarker =
        BitmapDescriptorFactory.fromBitmap(Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888))
    val marker = map.addMarker(
        MarkerOptions()
            .position(infoLatLng)
            .title(title)
            .snippet(message)
            .alpha(0f)
            .icon(invisibleMarker)
            .anchor(0f, 0f)
    )
    marker.showInfoWindow()
}

Use as follows:

polyline.addInfoWindow(map, "Title", "Message")

Example use:

polyline.addInfoWindow(map, route.distanceText, route.durationText)

enter image description here

like image 197
Simon Mayrshofer Avatar answered Oct 03 '22 14:10

Simon Mayrshofer