Esempio n. 1
0
  public Route(JSONObject obj) throws JSONException {
    this.duration = obj.getDouble("duration");
    this.length = obj.getDouble("length");

    JSONArray legs = obj.getJSONArray("legs");

    steps = new ArrayList<Route.RouteStep>();

    for (int i = 0; i < legs.length(); i++) {
      RouteStep s = new RouteStep();

      JSONObject leg = legs.getJSONObject(i);

      s.length = leg.getDouble("length");
      s.duration = leg.getDouble("duration");
      this.actual_duration += Math.ceil(s.duration / 60) * 60;

      try {
        s.desc = leg.getString("code");
      } catch (Exception e) {
        // Log.e("HelsinkiTravel", "That's ok");
      }
      ;

      try {
        s.type = leg.getInt("type");
      } catch (Exception e) {
        // Log.e("HelsinkiTravel", "That's ok");
      }
      ;

      JSONArray locs = leg.getJSONArray("locs");

      s.path = new ArrayList<Route.PathSegment>();

      for (int j = 0; j < locs.length(); j++) {
        JSONObject loc = locs.getJSONObject(j);
        JSONObject coord = loc.getJSONObject("coord");

        PathSegment p = new PathSegment();
        p.coords = new Coords(coord.getDouble("y"), coord.getDouble("x"));

        p.arrTime = parseReittiOpasDate(loc.getString("arrTime"));
        p.depTime = parseReittiOpasDate(loc.getString("depTime"));

        try {
          p.name = loc.getString("name");
        } catch (Exception e) {
          // Log.e("HelsinkiTravel", "That's ok");
        }
        ;

        if (p.name != null && !p.name.equals("nul")) {
          if (s.firstLoc.equals("")) s.firstLoc = p.name;
          s.lastLoc = p.name;
        }
        s.path.add(p);

        if (s.depTime == null && p.depTime != null) {
          s.depTime = parseReittiOpasDate(loc.getString("depTime"));
        }
        if (p.arrTime != null) {
          s.arrTime = parseReittiOpasDate(loc.getString("arrTime"));
        }

        if (this.depTime == null && p.depTime != null) {
          this.depTime = parseReittiOpasDate(loc.getString("depTime"));
        }
        if (p.arrTime != null) {
          this.arrTime = parseReittiOpasDate(loc.getString("arrTime"));
        }
        if (this.firstBusTime == null && p.depTime != null && s.type != 0) {
          this.firstBusTime = parseReittiOpasDate(loc.getString("depTime"));
        }
      }

      steps.add(s);
    }

    this.jsonString = obj.toString();
  }