How to draw/visualize data from JSON file?

Hi, so I have successfully imported my JSON file into processing, but now I am trying to draw plot point or lines that represent the longitude and latitude. I have tried searching on here for solutions, but I am just more confused now. I will paste my current code below...

JSONObject json, iss_position;
String message;
int timestamp;
float latitude, longitude;

void setup() {
  size(500, 500);
  noStroke();
}

void draw() {
  background(0);
  json = loadJSONObject("http://api.open-notify.org/iss-now.json");
  iss_position = json.getJSONObject("iss_position");
  message = json.getString("message");
  timestamp = json.getInt("timestamp");
  latitude = iss_position.getFloat("latitude");
  longitude = iss_position.getFloat("longitude");

  println(message + ", " + timestamp + ", " + latitude + ", " + longitude);

  ellipse(latitude, longitude, 10, 10);
  text("TIME: "+timestamp, 10,450);
  text("LATITUDE: "+latitude, 10,470);
  text("LONGITUDE "+longitude, 10,490);

}

Answers

  • Edit post, highlight code, press ctrl-o to format

    Don't load files within draw, draw is called 60 times a second and loading a file is too expensive to be called that often. It'll also annoying the website.

  • edited October 2017
    {
      "timestamp": 1507362311, 
      "message": "success", 
      "iss_position": {
        "longitude": "166.2819", 
        "latitude": "51.2426"
      }
    }
    

    Note also that latitude and longitude are within another element called iss_position

    Edit: ok, you've seen that. What does the output say, do you see sensible looking values?

  • edited October 2017

    yeah, have run that now and the json parsing looks fine. the problem is the domain and range - you need to map the numbers you get so that they are sensible screen coordinates.

    i just got

    success, 1507366342, -2.4234, 47.8786
    

    which is slightly off the screen in the x direction, 47 pixels down from the top. it also suggests that the x can be negative. in fact latitude (y) can go from -90 (south pole) to +90 (north pole) and longitude (x) can be from 0 to 360 (or possibly from -180 to 180).

    look at map() in the reference.

Sign In or Register to comment.