微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

折线从头到尾连接|谷歌地图 |安卓

如何解决折线从头到尾连接|谷歌地图 |安卓

问题是,当我绘制多段线时,它们从起点和终点连接起来,而不是连接到。我只是解码我的多点(它们是来自方向 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

}

See Polylines here

解决方法

可能在您的解码折线点中,起始位置坐标存在于第一个和最后一个点的两倍。因此,尝试删除列表中的最后一点:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。