我是Android的新手,我目前正在开发一个Android项目。我偶然发现了解码google-map加密map-String的问题。短字符串工作正常,长字符串崩溃也很糟糕。
这是我解码map-string的方法。
public static List<LatLng> decode2(String encoded) {
List<LatLng> track = new ArrayList<>();
int index = 0;
int lat = 0, lng = 0;
while (index < encoded.length()) {
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; // Error at this line.
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 );
track.add(p);
}
return track;
}
我以这种方式打电话给我的方法:
latLngArrayList = (ArrayList<LatLng>) PolyUtil.decode2(routes.get(groupPosition).getEncryptedMapString());
在通话中收到以下错误。
java.lang.StringIndexOutOfBoundsException: length=4438; index=4438;
at com.google.maps.android.PolyUtil.decode2(PolyUtil.java:314)
有问题的字符串:
argtJ}vg`A{F}Cw@aAi@eAy@oCmBeJWw@mBqDi@}Aw@aEoA_Iu@wC_BoDwB{CmAwB}@iAsBkBeAmAeE}Cg@MeCoB{CsDuJwMgHcG_E{DoHoKaByAq@_@iB]}CQu@@s@NyH~Bk@?k@O{AaBeAgBmDsD{CaEoD{HwE}IoDiGwCqEgB_D{HeM}CuFu@cAcAs@g@Mu@Ik@F_AdAqArCeAzA}@|@iB~@kADUGqBkA_EaGmEuE}DuDsB}B}FcJiDwEgAiA_C{AkB}AqCaEeAsBqA}C_@sAUyBQoDW_CYoBYw@]_@{Bm@gBuA{D{EmDyEy@qAeBqDwBcCgBcBa@Q]?_Bh@{@CcEkAw@i@u@{AsGiQa@w@YYkGqD_F{Eu@i@_A_@m@Ck@LwHbEk@P_A?q@Qs@m@e@oAWgBq@wLc@wDg@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如何解决这个问题将非常感激。
答案 0 :(得分:0)
从您粘贴的输出中,您可以看到索引确实超出范围:
length=4438; index=4438;
index
的有效排名为0至4437。
您正在index
- 循环中递增while
个不同位置:
b = encoded.charAt(index++) - 63;
在第一个do while
和第二个do while
中。
while
- 循环中的检查条件断言index
位置在执行第一次String encoded
- 操作时位于charAt()
内。
在第一个index
do while
上的后增量后,它可能已到达String encoded
的末尾。在第二个charAt()
中执行do while
时,这会导致StringIndexOutOfBoundsException。