Skip to content

Instantly share code, notes, and snippets.

@Biseamon
Created February 18, 2020 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Biseamon/64515eefec707a9c3cb4fcc38dec56a1 to your computer and use it in GitHub Desktop.
Save Biseamon/64515eefec707a9c3cb4fcc38dec56a1 to your computer and use it in GitHub Desktop.
JSON polyline points parser
public void prs(String s){
PolylineOptions polylineOptions = new PolylineOptions();
List<LatLng> movements = new ArrayList<>();
List<String> points = new ArrayList<>();
String point = "";
try {
JSONObject json = new JSONObject(s);
JSONArray jsonRoute = json.getJSONArray("routes");
int count = jsonRoute.length();
for (int i = 0; i < count; i++) {
JSONObject jsonObject = jsonRoute.getJSONObject(i);
JSONArray jsonArray = jsonObject.getJSONArray("legs");
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
JSONArray jsonArray1 = jsonObject1.getJSONArray("steps");
int count1 = jsonArray1.length();
for (int j = 0; j < count1; j++) {
JSONObject jsonObject2 = jsonArray1.getJSONObject(i);
point = jsonObject2.getJSONObject("polyline").getString("points");
points.add(point);
}
movements.addAll(PolyUtil.decode(String.valueOf(points)));
polylineOptions.width(10).color(Color.RED);
polylineOptions.addAll(movements);
mMap.clear();
mMap.addPolyline(polylineOptions);
Log.i("prs", "prs: " + movements.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment