Problem is that when I draw polylines, they get connected from start and end point which is not supoosed to. I simply decode my polypoints (which are encoded strings from direction api) and then add them to polyoptions to draw polylines on map.
Here is my code below:
val options = PolylineOptions()
options.color(Color.RED)
options.width(10f)
val list = booking.polyPointsList.flatMap { it.decodePoly() }
options.addAll(list)
whats inside decodepoly()
fun String.decodePoly(): MutableList<LatLng> {
if (this.isEmpty()) return mutableListOf()
val poly = ArrayList<LatLng>()
var index = 0
val len = this.length
var lat = 0
var lng = 0
while (index < len) {
var b: Int
var shift = 0
var result = 0
do {
b = this[index++].toInt() - 63
result = result or (b and 0x1f shl shift)
shift += 5
} while (b >= 0x20)
val dlat = if (result and 1 != 0) (result shr 1).inv() else result shr 1
lat += dlat
shift = 0
result = 0
do {
b = this[index++].toInt() - 63
result = result or (b and 0x1f shl shift)
shift += 5
} while (b >= 0x20)
val dlng = if (result and 1 != 0) (result shr 1).inv() else result shr 1
lng += dlng
val p = LatLng(
lat.toDouble() / 1E5,
lng.toDouble() / 1E5
)
poly.add(p)
}
return poly
}
Probably in your decoded polyline point with start location coordinates exists twice as first and last point. So, try to remove last point in your list:
val options = PolylineOptions()
options.color(Color.RED)
options.width(10f)
val list = booking.polyPointsList.flatMap { it.decodePoly() }
list.removeLast()
^^^^^^^^^^^^^^^^^ - add this line
options.addAll(list)
Or, may be you add all of polyline points twice (or more). In that case "first point" also exists in polyline points at least twice:
+--------------------- Cycle --------------------+
| |
first_point -> second_point -> ... last_point -> first_point -> ... -> last_point
\____________________ _____________________/ \__________ _________________/
V V
first time added points list second time added points list