【问题标题】:How to deal with 0,0 marker error in Google Map Direction API?如何处理 Google Map Direction API 中的 0,0 标记错误?
【发布时间】:2018-11-03 20:27:53
【问题描述】:

我尝试做一个 android 应用程序,我正在尝试使用 Google map Direction API 在 2 个标记之间画线,当我尝试解码来自 goole 服务器的 JSON 中的折线点时,我有一堆标记设置在位置 0.0,0.0 附近,就像我在下面的屏幕截图中一样

我的折线解码函数是

private fun decodePoly(polylineArrayList: ArrayList<String>): List<LatLng> {
    val poly = ArrayList<LatLng>()
    var index = 0
    for (i in 0 until polylineArrayList.size-1) {
        val encoded = polylineArrayList[i]
        val len = encoded.length
        var lat = 0
        var lng = 0

        while (index < len -1) {
            var b: Int
            var shift = 0
            var result = 0
            do {
                b = encoded[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 = encoded[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
}

这是我画线的代码(我在我的 asynck 任务中成功完成

private val options = PolylineOptions()
private val latLongB = LatLngBounds.Builder()
override fun onSuccess(googleDirectionData: GoogleDirectionData?) {
     val polylineArrayList = ArrayList<String>()
     for (route in googleDirectionData?.routes!!) {
          for (leg in route.legs) {
              for (step in leg.steps)
                  polylineArrayList.add(step.polyline.points)
          }
      }
      val polypts = decodePoly(polylineArrayList)
      for (point in polypts) {
          options.add(point)
          latLongB.include(point)
      }
val caisseMarker = LatLng(caisse.wsg84[0], caisse.wsg84[1])
options.add(caisseMarker)
latLongB.include(caisseMarker)
val bounds = latLongB.build()
// add polyline to the map
map.addPolyline(options)
// show map with route centered
map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100))
}

bunch of marker near 0.0,0.0

有人知道如何按顺序解决这个问题吗 不再有定位 0.0,0.0 的标记?

【问题讨论】:

  • 请将您的代码添加到您正在绘制的地图中
  • 您可以直接在帖子中添加该图片,这样阅读您的帖子会更有效率
  • 是的,我会更新我的帖子
  • R.F.Nelson 我不允许这样做......我不知道为什么

标签: android api google-maps kotlin marker


【解决方案1】:

如果您想提高性能,您应该使用预构建的库。

看看

https://github.com/jd-alexander/Google-Directions-Android

https://github.com/bkhezry/MapDrawingTools

https://github.com/akexorcist/Android-GoogleDirectionLibrary

简单的语法

GoogleDirection.withServerKey("YOUR_SERVER_API_KEY")
        .from(new LatLng(41.8838111, -87.6657851))
        .and(new LatLng(41.8766061, -87.6556908))
        .and(new LatLng(41.8909056, -87.6467561))
        .to(new LatLng(41.9007082, -87.6488802))
        .transportMode(TransportMode.DRIVING)
        .execute(new DirectionCallback() {
            @Override
            public void onDirectionSuccess(Direction direction, String rawBody) {
                if(direction.isOK()) {
                    // Do something
                } else {
                    // Do something
                }
            }

            @Override
            public void onDirectionFailure(Throwable t) {
                // Do something
            }
        });

【讨论】:

    【解决方案2】:

    是的,thanx,但我并不是真的在寻找性能,这只是一个实习项目,以便学习 kotlin,所以我真的不需要提高性能

    【讨论】:

      猜你喜欢
      • 2018-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-01
      • 2016-03-22
      • 1970-01-01
      • 2019-02-11
      相关资源
      最近更新 更多