What am I doing wrong? Geolocation displays incorrectly, getScreenPosition not working

edited February 2015 in Library Questions

I am trying to map geolocations (and other features) from live JSON feed, but the plotted locations are not displaying accurately i.e. they do not align with the Unfolding map base. I am trying to use getScreenPosition (second last line of the code) to fix this, but am getting "event.location cannot be solved or is not a field" message. I am a complete beginner at this, so probably missing something that is obvious to someone else. Can anyone tell me what I am doing wrong here? Thanks in advance! Here's the full code:

UnfoldingMap map;

void setup() {
  size(800, 600);
  map = new UnfoldingMap(this);
  map.zoomAndPanTo(new Location(41.9f, -87.7f), 10);
  MapUtils.createDefaultEventDispatcher(this, map);
}

JSONArray events;
String url = "http:" + "//divvybikes.com/stations/json";

float minLat = 41.77;
float maxLat = 41.99;
float minLon = -87.75;
float maxLon = -87.55;

void draw()
{
    if(frameCount%30==0 || frameCount==1)
    {
        getLatest();
    }
    drawLatest();
}

void getLatest()
{
    JSONObject rawEvents = loadJSONObject(url);
    events = rawEvents.getJSONArray("stationBeanList");

}

void drawLatest()
{
    fill(255,0,0);
    map.draw();

    for(int i =0; i<events.size(); i++)
    {
        JSONObject event = events.getJSONObject(i);
        float x = map(event.getFloat("longitude"), minLon, maxLon, 0, width);
        float y = map(event.getFloat("latitude"), minLat, maxLat, height, 0);
        float n = event.getFloat("availableBikes");
        Location location = new Location(x, y);
        ScreenPosition pos = map.getScreenPosition(event.location);     
        ellipse(pos.x, pos.y, n, n);
    }
}
Tagged:

Answers

  • See https://processing.org/reference/JSONObject.html
    You cannot access a Json attribute like this, you have to use getString() or getInt() or similar.

  • edited February 2015

    @kaisaph== as i dont know the place it is difficult !!! but no more error code...

    try this::

        import de.fhpotsdam.unfolding.mapdisplay.*;
        import de.fhpotsdam.unfolding.utils.*;
        import de.fhpotsdam.unfolding.marker.*;
        import de.fhpotsdam.unfolding.tiles.*;
        import de.fhpotsdam.unfolding.interactions.*;
        import de.fhpotsdam.unfolding.ui.*;
        import de.fhpotsdam.unfolding.*;
        import de.fhpotsdam.unfolding.core.*;
        import de.fhpotsdam.unfolding.mapdisplay.shaders.*;
        import de.fhpotsdam.unfolding.data.*;
        import de.fhpotsdam.unfolding.geo.*;
        import de.fhpotsdam.unfolding.texture.*;
        import de.fhpotsdam.unfolding.events.*;
        import de.fhpotsdam.utils.*;
        import de.fhpotsdam.unfolding.providers.*;
    
    
        UnfoldingMap map;
    
        void setup() {
          size(800, 600);
          map = new UnfoldingMap(this);
          map.zoomAndPanTo(new Location(41.9f, -87.7f), 12);
          MapUtils.createDefaultEventDispatcher(this, map);
        }
    
        JSONArray events;
        String url = "http:" + "//divvybikes.com/stations/json";
    
        float minLat = 41.77;
        float maxLat = 41.99;
        float minLon = -87.75;
        float maxLon = -87.55;
    
        void draw()
        {
            if(frameCount%30==0 || frameCount==1)
            {
                getLatest();
            }
            drawLatest();
        }
    
        void getLatest()
        {
            JSONObject rawEvents = loadJSONObject(url);
            events = rawEvents.getJSONArray("stationBeanList");
    
        }
    
        void drawLatest()
        {
            fill(255,0,0);
            map.draw();
    
            for(int i =0; i<events.size(); i++)
            {
                JSONObject event = events.getJSONObject(i);
                float x = map(event.getFloat("longitude"), minLon, maxLon, 0, width);
                float y = map(event.getFloat("latitude"), minLat, maxLat, height, 0);
                float n = event.getFloat("availableBikes");
                Location location = new Location(x, y);
                ScreenPosition pos = new ScreenPosition(location.x, location.y);
    
               // ScreenPosition pos = map.getScreenPosition(event.location);     
               ellipse(pos.x, pos.y, n, n);
    
            }
        }
    
  • if(frameCount%30==0 || frameCount==1)

    you're reading and parsing that json file twice a second? that feels like way too much.

    and you're redrawing 60 times a second despite the data not having changed? that also seems like a waste.

  • Thanks for these suggestions akenaton and PhiLho. Unfortunately the problem still persists. Anyone with further thoughts?

    Akenaton: The markers are still not aligned properly and the outcome is the same as before adding the lines about new Location and ScreenPosition. I've also tried creating simple markers following this tutorial, but just got error messages even when trying to reproduce the example with RSS feed.

    PhiLho: Not sure what you mean about using getString etc for accessing json attributes. Can you give an example how you would use that to get the lat and lon to line up with the base map?

    Here's the screenshot of the current situation

    Screen Shot 2015-02-07 at 11.16.03

  • Of course dear watson!!!! lat = pos.x!!! So=== see if its right...as i dont know the places i cannot be sure...but now they are no more swiming

        import de.fhpotsdam.unfolding.mapdisplay.*;
    import de.fhpotsdam.unfolding.utils.*;
    import de.fhpotsdam.unfolding.marker.*;
    import de.fhpotsdam.unfolding.tiles.*;
    import de.fhpotsdam.unfolding.interactions.*;
    import de.fhpotsdam.unfolding.ui.*;
    import de.fhpotsdam.unfolding.*;
    import de.fhpotsdam.unfolding.core.*;
    import de.fhpotsdam.unfolding.mapdisplay.shaders.*;
    import de.fhpotsdam.unfolding.data.*;
    import de.fhpotsdam.unfolding.geo.*;
    import de.fhpotsdam.unfolding.texture.*;
    import de.fhpotsdam.unfolding.events.*;
    import de.fhpotsdam.utils.*;
    import de.fhpotsdam.unfolding.providers.*;
    
    
    UnfoldingMap map;
    
    void setup() {
      size(800, 600);
      map = new UnfoldingMap(this);
      map.zoomAndPanTo(new Location(41.9f, -87.7f), 10);
      MapUtils.createDefaultEventDispatcher(this, map);
    }
    
    JSONArray events;
    String url = "http:" + "//divvybikes.com/stations/json";
    
    float minLat = 41.77;
    float maxLat = 41.99;
    float minLon = -87.75;
    float maxLon = -87.55;
    
    void draw()
    {
        if(frameCount%30==0 || frameCount==1)
        {
            getLatest();
        }
        drawLatest();
    }
    
    void getLatest()
    {
        JSONObject rawEvents = loadJSONObject(url);
        events = rawEvents.getJSONArray("stationBeanList");
    
    }
    
    void drawLatest()
    {
        fill(255,0,0);
        map.draw();
    
        for(int i =0; i<events.size(); i++)
        {
            JSONObject event = events.getJSONObject(i);
            float x = map(event.getFloat("longitude"), minLon, maxLon, 0, width);
            float y = map(event.getFloat("latitude"), minLat, maxLat, height, 0);
            float n = event.getFloat("availableBikes");
            Location location = new Location(x, y);
            ScreenPosition pos = new ScreenPosition(location.x, location.y);
    
           // ScreenPosition pos = map.getScreenPosition(event.location);     
           ellipse(pos.y, pos.x, n, n);
    
        }
    }
    
  • JSONObject event = events.getJSONObject(i);
    // [...]
    ScreenPosition pos = map.getScreenPosition(event.location);
    

    event is a JSONObject, a Java object holding a bunch of data. Java is statically typed, you cannot add a field to an object, so event.location cannot be used, because location isn't a field of JSONObject, it is only a property name in this object. So you have to access it by its name using the aforementioned functions.

  • @PhiLho===

    right; but i dont call (event.location), i create Location location and add to it as x & y the values extracted by getFloat(name). What is wrong doing that? - And the code i posted seems to work without throwing any error...

  • edited February 2015

    @PhiLho Thanks. I still can't work out how to make the location markers 'stick' to the UnfoldingMap base and if I can't use the map.getScreenPosition(event.location) method with JSONObject I'm not sure what to do. I tried to create new markers based on advice on Unfolding Maps website but got error messages when trying to use List Feature. I have been trying to work this out for the last two weeks and I am thoroughly stuck. Thanks for all your help so far!

    @akenaton Thanks for your suggestion. I tried it but the markers are still not aligned with the map base and when zooming into the map the markers stay still. There is probably some elegant solution to this, but I haven't come across it yet. Thanks for your help!

  • @kaisaph::

    can you post the code you are NOW using?

  • Ok, I've managed to fix it now. Had to create a marker class. Current version of the map:

    Screen Shot 2015-02-27 at 17.48.17

  • @akenaton: I was still commenting on the original code... :-)

  • @Kaisaph could you elaborate on your fix? I have created a class that implement Marker, but I still observe similar problem as you did. I use map.getScreenPosition(Location) but the coordinates I am getting seems to be shifted by a fixed offset.

Sign In or Register to comment.