我有一个带有 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
?是我的字符串无效吗?如何检查编码的折线字符串是否有效?有人可以给我发一个有效编码折线的有效示例吗?
最佳答案
您传递给 PolyUtil.decode()
的编码 String
似乎不是有效的编码折线。好像是UUID .
IndexOutOfBoundsException
是由无效的编码折线引起的。
这里是编码/解码折线的例子:
// 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 中找到更多关于编码和解码折线的示例和信息。 .
关于java - Android 谷歌地图 API : IndexOutOfBoundsException in PolyUtil. 解码(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33433415/