In my exporter class I have create a HashMap and add my Parser ID as key but I have no idea what the 2nd Parameter is - I have set it to null.
final HashMap<string, object=""> extParserList = new HashMap<string, object="">();
extParsersList.put(GpsLogExtensionParser.PARSER_ID, null);
track.setExtensionData(extParserList);</string,></string,>
The code seems to work OK but what is the 2nd parameter for? I thought it be an object I could pass to the IExtension parse class but this doesn't seem to be the case.
I would also like to set extension data for each track point but how can I pass data from my importer class to the IEntensionParser class?
In order to get information from the "extensions" node, you need to write a class that implements the IExtensionParser interface. This class will define methods for handling "extensions" data from all standard gpx entities (trk, rte, wpt). After you have implemented your class, you need to register it with the GPXParser:
GPXParser p = new GPXParser();
p.addExtensionParser(an instance of your parser class);
This is very important because this will make GPXParser aware of your custom parser.
You are on the right track to implement the IExtensionParser interface with the following code:
@Override
public void writeTrackExtensionData(Node node, Track wpt, Document doc) {
// append node1
final Node node1 = doc.createElement("Node1");
node1.setTextContent(mNode1Value);
node.appendChild(distanceNode);
// append node2
final Node node2 = doc.createElement("Node2");
node2.setTextContent(mNode2Value);
node.appendChild(node2);
}
Now, form what I see in your code, you need to add this custom parser to GPXParser:
GPXParser p = new GPXParser();
p.addExtensionParser(new GpsLogExtensionParser());
This will call your custom parser when writing the GPX structure to a file.
In order to create a waypoint with extension information, you need to add some useful information to be written as custom data:
Waypoint p = new Waypoint();
....
p.addExtensionData(GpsLogExtensionParser.PARSER_ID, "some useful information that you will want to write in the gpx file using the custom extension parser");
The second parameter can be any java object that you will use to pass information to the custom parser.
In the parser (GpsLogExtensionParser), you need to use:
public void writeWaypointExtensionData(Node node, Waypoint wpt, Document doc);
String myData = (String)wpt.getExtensionData(GpsLogExtensionParser.PARSER_ID)
you will have access to the extension data that you stored in your waypoint bean.
After that, you can use that information to process it the way you want it.
Hope this helps,
Ghita
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
Thanks for a great library. It has helped me a lot but I have a few questions.
Is there an example available using setExtensionData? So far I have extended IExtensionParser and implemented writeTrackExtensionData (below):
@Override
public void writeTrackExtensionData(Node node, Track wpt, Document doc) {
// append node1
final Node node1 = doc.createElement("Node1");
node1.setTextContent(mNode1Value);
node.appendChild(distanceNode);
In my exporter class I have create a HashMap and add my Parser ID as key but I have no idea what the 2nd Parameter is - I have set it to null.
final HashMap<string, object=""> extParserList = new HashMap<string, object="">();
extParsersList.put(GpsLogExtensionParser.PARSER_ID, null);
track.setExtensionData(extParserList);</string,></string,>
The code seems to work OK but what is the 2nd parameter for? I thought it be an object I could pass to the IExtension parse class but this doesn't seem to be the case.
I would also like to set extension data for each track point but how can I pass data from my importer class to the IEntensionParser class?
I want something like this:
I appreciate any help anyone can offer!
Thanks,
Andy
In order to get information from the "extensions" node, you need to write a class that implements the IExtensionParser interface. This class will define methods for handling "extensions" data from all standard gpx entities (trk, rte, wpt). After you have implemented your class, you need to register it with the GPXParser:
GPXParser p = new GPXParser();
p.addExtensionParser(an instance of your parser class);
This is very important because this will make GPXParser aware of your custom parser.
You are on the right track to implement the IExtensionParser interface with the following code:
@Override
public void writeTrackExtensionData(Node node, Track wpt, Document doc) {
// append node1
final Node node1 = doc.createElement("Node1");
node1.setTextContent(mNode1Value);
node.appendChild(distanceNode);
// append node2
final Node node2 = doc.createElement("Node2");
node2.setTextContent(mNode2Value);
node.appendChild(node2);
}
Now, form what I see in your code, you need to add this custom parser to GPXParser:
GPXParser p = new GPXParser();
p.addExtensionParser(new GpsLogExtensionParser());
This will call your custom parser when writing the GPX structure to a file.
In order to create a waypoint with extension information, you need to add some useful information to be written as custom data:
Waypoint p = new Waypoint();
....
p.addExtensionData(GpsLogExtensionParser.PARSER_ID, "some useful information that you will want to write in the gpx file using the custom extension parser");
The second parameter can be any java object that you will use to pass information to the custom parser.
In the parser (GpsLogExtensionParser), you need to use:
public void writeWaypointExtensionData(Node node, Waypoint wpt, Document doc);
String myData = (String)wpt.getExtensionData(GpsLogExtensionParser.PARSER_ID)
you will have access to the extension data that you stored in your waypoint bean.
After that, you can use that information to process it the way you want it.
Hope this helps,
Ghita