jorjSB
jorjSB

Reputation: 610

Android Google Maps Api V2 PolyLine decode Index Out Of Bound Exception

Hy all. I have a question about drawing a google maps route(walking, driving, transit)..

So: I call an url ex: String urlToCall = "http://maps.googleapis.com/maps/api/directions/json" + "?origin="+getMyLat() + "," + getMyLong() + "&destination="+ Double.parseDouble(eventLocationLat) + "," + Double.parseDouble(eventLocationLong) + "&sensor=true" + "&language=" + Locale.getDefault().getLanguage() + "&departure_time=" + System.currentTimeMillis()/1000 + "&mode=" + getMovingMode();

It's all good, and returns an JSON. From here, the next step for me is to draw an overline, showing the entire route, so:

private void drawRouteOnMap(String httpResult) {

    JSONObject routeObject = null;
    try {
        routeObject = new JSONObject(httpResult).optJSONArray("routes").optJSONObject(0);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String polyLine = null;
    try {
        polyLine = routeObject.optJSONObject("overview_polyline").getString("points");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if(polyLine != null){
        //  Log.e("polyLine", polyLine);
        setDecodedPoly( GoogleMapsCustomHelpers.decodePoly( polyLine ) );
    }

    setUpMapIfNeeded();
} 

All fine until here, but it gives an "IndexOutOfBoundsexception" in the "decodePoly" method. I commented on the line witch throws the exception:

public static List decodePoly(String encoded) {

    List<LatLng> poly = new ArrayList<LatLng>();
    int index = 0, len = encoded.length();
    int lat = 0, lng = 0;

    while (index < len) {
        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: INDEX OUT OF BOUND
            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)));
        poly.add(p);
    }

    return poly;
}

So, my question: what is going wrong? I didn't found any classes from google maps to handle PolyLine decoding, nothing.. I don't know what's wrong. Testing with an online decoder(based on V3 I think, all the polylines are drawn ok on map, but in my code they crash..)

The only difference I found is that in my code I have: // efovGyqkrCvBxDu@jAmAzAhAnBPEPBTTf@f@rCvCeAtBo@fB@NdCbClDeFfCqD@q@w@mAk@oA[yA?g@BOJYl@w@n@q@WD?JAn@dAP@^e@

and it crashes, but when I test this in "https://developers.google.com/maps/documentation/utilities/polylineutility", it's drawn correct, and it adds an "@" to the encoded polyLine. In other cases it added an "s@"

Can anybody tell me what I'm doing wrong? Thanks in advance!

Upvotes: 2

Views: 1244

Answers (1)

Anbarasan Thangapalam
Anbarasan Thangapalam

Reputation: 1018

I successfully solved the problem. We should unescape any Java literals found in the String poly line. It can be done using StringEscapeUtils.unescapeJava(polyLine)

The above can be changed into

if(polyLine != null){
                //  Log.e("polyLine", polyLine);
                polyLine  = StringEscapeUtils.unescapeJava(polyLine);
                setDecodedPoly( GoogleMapsCustomHelpers.decodePoly( polyLine ) );
            }

Upvotes: 2

Related Questions