Android GoogleMaps API: PolyUtil.decode() 中的 IndexOutOfBoundsException

Android GoogleMaps API: IndexOutOfBoundsException in PolyUtil.decode()

我有一个带有 GoogleMap 的 Android 应用程序,我想在上面画一条折线。为此,我使用 PolyUtils.decode()。但是如果用我的编码字符串调用它,我会得到一个 IndexOutOfBoundsException.

这是我的一段代码:

private void drawPolyLine() {
    List<Trail> trails = mVehicle.getTrails();
    for (Trail trail : trails) {
        String encodedPolyline = trail.getTrail();
        String color = trail.getColor();
        List<LatLng> locations = PolyUtil.decode(encodedPolyline);
        Polyline line = mMap.addPolyline(new PolylineOptions()
                .add(locations.toArray(new LatLng[locations.size()]))
                .width(5)
                .color(parseColor(color)));
    }
}

传递给 PolyUtil.decode() 的编码字符串是:

"0ee24344-9647-4592-92e7-a51b71008f4b"

堆栈跟踪如下所示:

java.lang.StringIndexOutOfBoundsException: length=36; index=36
 E/AndroidRuntime:     at java.lang.String.indexAndLength(String.java:500)
 E/AndroidRuntime:     at java.lang.String.charAt(String.java:494)
 E/AndroidRuntime:     at com.google.maps.android.PolyUtil.decode(PolyUtil.java:313)
 E/AndroidRuntime:     at net.my.domain.views.fragments.VehicleMapFragment.drawPolyLine(VehicleMapFragment.java:140)
 ...

什么可能导致这样的 IndexOutOfBoundsException?是我的字符串无效吗?如何检查编码的折线字符串是否有效?谁能 post 给我一个有效编码折线的有效示例?

您传递给 PolyUtil.decode() 的编码 String 似乎不是有效的编码折线。好像是UUID.

IndexOutOfBoundsException 是由无效的编码折线引起的。

这里有一个 encoding/decoding 折线的例子:

// Encoding
List<LatLng> locations2Encode = new ArrayList<LatLng>();
locations2Encode.add(new LatLng(40.1d, -3.1d));
locations2Encode.add(new LatLng(40.2d, -3.2d));
locations2Encode.add(new LatLng(40.3d, -3.3d));
locations2Encode.add(new LatLng(40.4d, -3.4d));
String encodedPolyline = PolyUtil.encode(locations2Encode);

// Decoding
List<LatLng> locations = PolyUtil.decode(encodedPolyline);

在这种情况下,表示编码折线的字符串是

_`wsF~m|Q_pR~oR_pR~oR_pR~oR

根据文档,似乎没有办法检查编码的多段线是否有效而不是解码它。

您可以在 the documentation 中找到更多关于编码和解码折线的示例和信息。

我们从该方向响应中获得的一些编码 "overview_polyline" 已编码,具有双编码反斜杠。要在静态地图请求中工作,需要将双 \ 转换为 \

尝试在这里交谈:https://developers.google.com/maps/documentation/utilities/polylineutility