我有多个与PolyUtil编码中的路线相对应的点。我在网络和本机android应用程序中显示了此路由,问题是在我的网络中显示正确,但在android应用程序中显示了一些不同。
我记录了两个PolyUtil解码,并且解码的点不同。使用PolyUtil交互式站点,我可以获得正确的路线。我认为问题出在android库上。
PolyUtil编码点:
lvzsEhgvuI~c@HbJr@lJ`@|MrDfJlE~GxCdEvQ~IhCfDh@bIi@dGwB|D{GL{VqBqGcHcEeNGqGpGmHnQ
android点已解码:
-34.88199, -56.15223
-34.883770000000005, -56.152280000000005
-34.885600000000004, -56.15254
-34.88799, -56.152710000000006
-34.889790000000005, -56.15361000000001
-34.89123, -56.15464000000001
-34.89222, -56.15541
-34.893980000000006, -56.15841
-34.89482, -56.1591
-34.896440000000005, -56.159310000000005
-34.89775, -56.1591
-34.898700000000005, -56.158500000000004
-34.898770000000006, -56.15708000000001
-34.8982, -56.15326
-34.89674, -56.15189
-34.894310000000004, -56.15091
-34.89294, -56.150870000000005
-34.89143, -56.152240000000006
-34.891290000000005, -56.15520000000001
解码的点数:
-34.876070000000006,-56.152370000000005
-34.88199,-56.152420000000006
-34.883770000000005,-56.152680000000004
-34.885600000000004,-56.15285000000001
-34.88799,-56.15375
-34.889790000000005,-56.15478
-34.89123,-56.155550000000005
-34.89222,-56.158550000000005
-34.893980000000006,-56.159240000000004
-34.89482,-56.15945000000001
-34.896440000000005,-56.159240000000004
-34.89775,-56.158640000000005
-34.898700000000005,-56.15722
-34.898770000000006,-56.153400000000005
-34.8982,-56.15203
-34.89674,-56.151050000000005
-34.894310000000004,-56.15101000000001
-34.89294,-56.15238000000001
-34.89143,-56.15534
在我的网络中,我这样解码:
google.maps.geometry.encoding.decodePath(encoded_estimated_wp);
在android应用中我这样解码:
PolyUtil.decode(encoded_path);
我该如何修复Android路线?显示不正确。
答案 0 :(得分:1)
类似的代码:
List <LatLng> pathPoints = PolyUtil.decode("lvzsEhgvuI~c@HbJr@lJ`@|MrDfJlE~GxCdEvQ~IhCfDh@bIi@dGwB|D{GL{VqBqGcHcEeNGqGpGmHnQ");
for (LatLng point : pathPoints) {
Log.d(TAG, "" + point.latitude + "," + point.longitude);
}
PolylineOptions polylineOptions = new PolylineOptions()
.addAll(pathPoints)
.color(Color.RED)
.width(20);
解码折线
lvzsEhgvuI~c@HbJr@lJ`@|MrDfJlE~GxCdEvQ~IhCfDh@bIi@dGwB|D{GL{VqBqGcHcEeNGqGpGmHnQ
与您的网络(google.maps.geometry.encoding.decodePath(encoded_estimated_wp);
)完全相同:
-34.876070000000006,-56.152370000000005
-34.88199,-56.152420000000006
-34.883770000000005,-56.152680000000004
-34.885600000000004,-56.15285000000001
-34.88799,-56.15375
-34.889790000000005,-56.15478
-34.89123,-56.155550000000005
-34.89222,-56.158550000000005
-34.893980000000006,-56.159240000000004
...
因此,请检查您的android-maps-utils
在应用buld.gradle
中的文件依赖性,并添加最新的version(目前为0.5 +):
dependencies {
...
implementation 'com.google.maps.android:android-maps-utils:0.5+'
}
或使用自定义decodePoly()
方法,如this的Marlon答案(实际上是从android-map-utils
抓取),并给出相同的结果:
private List<LatLng> decodePoly(String encoded) { List<LatLng> poly = new ArrayList<>(); int index = 0, len = encoded.length(); int lat = 0, lng = 0; while (index < len) { int b, shift = 0, result = 0; do { b = encoded.charAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); lat += dlat; shift = 0; result = 0; do { b = encoded.charAt(index++) - 63; result |= (b & 0x1f) << shift; shift += 5; } while (b >= 0x20); int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); lng += dlng; LatLng p = new LatLng((((double) lat / 1E5)), (((double) lng / 1E5))); poly.add(p); } return poly; }
答案 1 :(得分:0)
(不是预期的答案,没有代表要发表的意见:)
您可以转换Android版本的输入吗?像加/乘一个因子以获得相同的解码点?