android - 多段线从起点和终点连接起来 |谷歌地图|安卓

标签 android google-maps kotlin google-polyline

问题是,当我绘制折线时,它们从起点和终点连接,这是不应该的。我只需解码我的多点(它们是来自方向 api 的编码字符串),然后将它们添加到多选项以在 map 上绘制多段线。

下面是我的代码:

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

关于android - 多段线从起点和终点连接起来 |谷歌地图|安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67466966/

相关文章:

java - 如何从 Assets 文件夹创建文件对象?

ruby-on-rails - 给定特定的纬度/经度,如何计算出与纽约市附近地铁入口的距离?

ios - 如何在 iOS 的谷歌地图上使用 CLLocationManager 生成我的旅行路径?

generics - 如何为具有类型参数的类编写辅助构造函数?

Android EditText 协程去抖操作符,如 RxJava

android - 我可以在不解密的情况下使用 JSch 获取加密的 SSH 私钥的类型或指纹吗?

Android 文字覆盖图片

android - Android开发网站中的OBB(Opaque Binary Blob)是什么?

android - 手机和平板电脑的用户界面设计

javascript - 向 Google map 添加信息标记