Calendar prevTime = null;
Location prevLocation = null;
// Create track with all track points
ShapeFeature trackFeature = new ShapeFeature(FeatureType.LINES);
List<Double> speedList = new ArrayList<Double>();
XML[] itemXML = gpx.getChildren("trk/trkseg/trkpt");
for (int i = 0; i < itemXML.length; i++) {
// Adds location for track point
float lat = itemXML[i].getFloat("lat");
float lon = itemXML[i].getFloat("lon");
Location location = new Location(lat, lon);
// Calculates speed for track point
// Uses time span (h) and distance (km) to previous point to get km/h
double speed = 0;
try {
String timeStr = itemXML[i].getChild("time").getContent();
// Replace "Z" for Zulu/GMT time with parseable hour offset
timeStr = timeStr.replaceAll("Z", "+0000");
Calendar time = StringUtils.parseIsoDateTime(timeStr);
if (prevTime != null) {
long timeMS = time.getTimeInMillis();
long timeDiff = timeMS - prevTime.getTimeInMillis();
double dist = GeoUtils.getDistance(location, prevLocation);
speed = dist / ((float) timeDiff / 1000 / 60 / 60);
}
prevTime = time;
prevLocation = location;
} catch (ParseException e) {
// println("Error:" + e);
}
speedList.add(speed);
trackFeature.addLocation(location);
}
trackFeature.putProperty("speedList", speedList);
trackFeatures.add(trackFeature);
return trackFeatures;
}