1

When using Google Maps PolyUtil.decode it gives me this error java.lang.StringIndexOutOfBoundsException: length=60; index=60 at java.lang.String.charAt(Native Method) at com.google.maps.android.PolyUtil.decode(PolyUtil.java:464) When I trace the error it gives me the PolyUtil.class which then takes me to this particular line

do{
  b = encodedPath.charAt(index++) - 63 - 1;
  result += b << shift;
  shift += 5;
} while(b >= 31);

But so far this error is only thrown for a particular encoded string. When I decode another encoded string of the same length in characters or one that is longer or shorter in length it doesn't throw the error. I've even tested the string that gives the error using Google's Interactive Polyline Decoder Tool and it shows up properly. Any reason why this error is being thrown happen?

lifespunchingbag
  • 129
  • 1
  • 3
  • 12
  • Have you got any workaround? – sreenadh Jan 31 '20 at 12:47
  • Check the encoded string to see if there are any invalid characters. I figured it out after noticing that there were some escape characters missing such as `\\n`. In my case `\n` in the encoded string was throwing the error but when I changed it to `\\n` then it was able to properly decode. – lifespunchingbag Jan 31 '20 at 15:14
  • mc}r@ejvtMC\\\\XfAv@zDrAvGZ~ADVK?kBgJ{@iEi@kBQc@q@kAwAeCiAkBQEEAKEGIEM?KDOROR?LDBBN?pA?vBGv@Bp@FhBVxD`@GXIJa@`@uA~@wEvCSv@ (This is a sample of my encoded string) It consists of escape slashes. When I remove the back slashes,it shows "StringIndexOutOfBoundsException" and the path drawn is weird. Not drawing through the roads. It is not in all cases. – sreenadh Feb 03 '20 at 12:13
  • I would suggest that you redraw the line to make sure that you have it correct, copy it and then repaste it back into the tool to ensure it is what was drawn. Then save that encoded string somewhere and then paste it into to PolyUtil decode. Maybe somewhere along the way something happened to it. If it still gives the error with the new string then paste it here and I'll check it. – lifespunchingbag Feb 03 '20 at 15:32

2 Answers2

2

The issue is the double back slashes "\\". Just replace them with a single back slash "\", before decoding.
As followed:

String newEncodedString = encodedString.replace("\\\\","\\");
PolyUtil.decode(newEncodedString);
Taylor
  • 21
  • 2
2
    try {
       PolyUtil.decode(encodedFullPath)
    } catch (e: Exception) {
       val modifiedEncodedPath = "$encodedFullPath@"
        PolyUtil.decode(modifiedEncodedPath)
    }

fix for some encoded paths, used by:
https://developers.google.com/maps/documentation/utilities/polylineutility


explanation can be found here
https://developers.google.com/maps/documentation/utilities/polylinealgorithm
Jibin Joy
  • 21
  • 3