How to parse polyline and points using Jackson

7 views
Skip to first unread message

Computer Gig

unread,
Feb 19, 2020, 4:44:06 PM2/19/20
to jackson-user
I want to parse the entire JSON file that contains many JSONObjects "steps". I need to extract all polylines and their points. However, it returns only the very first value and nothing more. Is anything wrong with my loop or I just parse the JSON file wrongly? Thx

public void parseDirect(String response) {

        PolylineOptions polylineOptions = new PolylineOptions();
        List<LatLng> movements = new ArrayList<>();
        try{
            JSONObject json = new JSONObject(response);
            //Retrieve routes from response
            JSONObject jsonRoute = json.getJSONArray("routes").getJSONObject(0);
            //Retrieve legs from routes
            JSONObject legs = jsonRoute.getJSONArray("legs").getJSONObject(0);
            //Retrieve steps from legs
            JSONArray steps = legs.getJSONArray("steps");
            final int numSteps = steps.length();
            JSONObject step;
            //Retrieve points from steps
            for (int i = 0; i < numSteps; i++) {
                step = steps.getJSONObject(i);
                String pontos = step.getJSONObject("polyline").getString("points");
                movements.addAll(PolyUtil.decode(pontos));
            }

//            movementsTest.add(new LatLng(52.4634252,-1.8861262));
//            movementsTest.add(new LatLng(52.4761324,-1.8608113));
//            movementsTest.add(new LatLng(52.4634252,-1.8689613));

            polylineOptions.color(Color.RED);
            polylineOptions.width(10);
            polylineOptions.addAll(movements);

            mMap.clear();
            mMap.addPolyline(polylineOptions);
            Log.i("parser", "parseDirect: " + movements.toString());
        }catch(Exception ex){
            Log.d("DirectionErr",ex.getMessage());
        }

        //make use of movements object here
    }


Reply all
Reply to author
Forward
0 new messages