lifespunchingbag
lifespunchingbag

Reputation: 129

PolyUtil.decode Throwing StringIndexOutOfBoundsException

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?

Upvotes: 1

Views: 984

Answers (2)

Jibin Joy
Jibin Joy

Reputation: 21

    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

Upvotes: 2

Taylor
Taylor

Reputation: 21

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);

Upvotes: 2

Related Questions