I've constructed this code based on the principles of Daniel Schiffmans 'exercise_18_15_new_bubbles_xml.pde' on Github.
All is going well, except that I am having difficulty applying it using Unfolding Maps to display train locations from an XML file.
I wish to display either a 'SimplePointMarker' (from the Unfolding Maps library) or a simple ellipse for each train location. My difficulty is with the mapping. I can map one train with no issues but unsure how to map all within the array.
I have tried a few things like making an array of train locations separately and making another object to handle mapping but both have failed.
The code is posted below. The core issue lies on line 80.
The XML data is contained in this link. I copied it to a static XML file: https://data.smartdublin.ie/cgi-bin/rtpi/realtimebusinformation?stopid=7602&format=xml
Any help would be greatly appreciated. Thank you :)
import de.fhpotsdam.unfolding.*;
import de.fhpotsdam.unfolding.geo.*;
import de.fhpotsdam.unfolding.utils.*;
import de.fhpotsdam.unfolding.marker.*;
UnfoldingMap map;
SimplePointMarker myMarker; // ???
Train[] trains;
XML xml; // A Table object
void setup() {
size(800, 600, P2D);
map = new UnfoldingMap(this);
MapUtils.createDefaultEventDispatcher(this, map);
loadData();
}
void draw() {
background(255);
// Display all bubbles
for (int i = 0; i < trains.length; i++) {
trains[i].display();
}
}
void loadData() {
xml = loadXML("xml.xml"); // Load XML file
XML[] children = xml.getChildren("objTrainPositions"); // Get all the child nodes named "objTrainPositions"
// The size of the array of Train objects is determined by the total XML elements named "bubble"
trains = new Train[children.length];
for (int i = 0; i < trains.length; i++) {
XML trainCodeElement = children[i].getChild("TrainCode");
String trainCode = trainCodeElement.getContent();
XML latitudeElement = children[i].getChild("TrainLatitude");
float latitude = latitudeElement.getFloatContent();
XML longitudeElement = children[i].getChild("TrainLongitude");
float longitude = longitudeElement.getFloatContent();
println("Train Code: " + trainCode + ", Latitude: " + latitude + ", Longitude: " + longitude);
Location trainLocation = new Location(latitude, longitude);
trains[i] = new Train(trainLocation, 50, trainCode); // Make a Train object out of the data read
}
}
class Train {
PVector location;
float diameter;
String name;
boolean over = false;
// Create the Train
Train (PVector location_, float diameter_, String s) {
location = location_;
diameter = diameter_;
name = s;
}
void display() { // Display the Train
stroke(150);
strokeWeight(2);
noFill();
ScreenPosition screenPos = myMarker.getScreenPosition(map); // ***NOT SURE HOW TO PROCEEED***
//berlinMarker = new SimplePointMarker(berlinLocation);
fill(150);
ellipse(screenPos.x, screenPos.y, 36, 36);
}
}