Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. /**
  2. * Parsing the response from api.
  3. * TODO note to the backend - in response should be the coordinates of the returned area.
  4. * This would easiely allow you to detect any problems with the map (gps or network problem etc.)
  5. */
  6. public Roads parseRoads(String response) {
  7. Roads roads = new Roads();
  8. try {
  9. JSONArray ja = new JSONObject(response).getJSONArray(FEATURES);
  10. for (int i=0;i<ja.length();i++) {
  11. Road road = new Road();
  12. JSONObject jo = ja.getJSONObject(i);
  13. JSONObject geometry = jo.getJSONObject(GEOMETRY);
  14. JSONObject prop = jo.getJSONObject(PROPERTIES);
  15. if (prop.has(CATEGORY)) road.category = prop.getString(CATEGORY);
  16. if (prop.has(DIRECTION)) road.direction = prop.getInt(DIRECTION);
  17. try {
  18. if (prop.has(SPEED_LIMIT_ALONG)) road.speedLimitAlong = prop.getInt(SPEED_LIMIT_ALONG);
  19. } catch (Exception e) {
  20. //e.printStackTrace();
  21. road.speedLimitAlong = SPEED_LIMIT_NO_VALUE;
  22. }
  23. try {
  24. if (prop.has(SPEED_LIMIT_AGAINST)) road.speedLimitAgainst = prop.getInt(SPEED_LIMIT_AGAINST);
  25. } catch (Exception e) {
  26. //e.printStackTrace();
  27. road.speedLimitAgainst = SPEED_LIMIT_NO_VALUE;
  28. }
  29. JSONArray cord = geometry.getJSONArray(COORDINATES);
  30. for (int j=0;j<cord.length();j++) {
  31. double lon = cord.getJSONArray(j).getDouble(0);
  32. double lat = cord.getJSONArray(j).getDouble(1);
  33. road.coordinates.add(new Place(lon,lat));
  34. }
  35. roads.add(road);
  36. }
  37. } catch (JSONException e) {
  38. Log.e(TAG, "parseRoads: "+e.getMessage());
  39. e.printStackTrace();
  40. }
  41. return roads;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement