问题描述
问题是,当我绘制多段线时,它们从起点和终点连接起来,而不是连接到。我只是解码我的多点(它们是来自方向 api 的编码字符串),然后将它们添加到 polyoptions 以在地图上绘制多段线。
这是我的代码如下:
val options = polylineoptions()
options.color(Color.RED)
options.width(10f)
val list = booking.polyPointsList.flatMap { it.decodepoly() }
options.addAll(list)
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
}
解决方法
可能在您的解码折线点中,起始位置坐标存在于第一个和最后一个点的两倍。因此,尝试删除列表中的最后一点:
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)
或者,您可能将所有折线点添加两次(或更多)。在这种情况下,“第一点”也至少存在于折线点中两次:
+--------------------- Cycle --------------------+
| |
first_point -> second_point -> ... last_point -> first_point -> ... -> last_point
\____________________ _____________________/ \__________ _________________/
V V
first time added points list second time added points list