Skip to content
This repository has been archived by the owner on Apr 23, 2023. It is now read-only.

Commit

Permalink
Adds speed to mock locations broadcast from a GPX trace
Browse files Browse the repository at this point in the history
  • Loading branch information
ecgreb committed Sep 30, 2014
1 parent 40b85e8 commit 8cc43ea
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lost/src/main/java/com/mapzen/android/lost/LocationClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class LocationClient {

// GPX tags
public static final String TAG_TRACK_POINT = "trkpt";
public static final String TAG_SPEED = "speed";
public static final String TAG_LAT = "lat";
public static final String TAG_LNG = "lon";

Expand Down Expand Up @@ -269,13 +270,17 @@ public void run() {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
final XPath xPath = XPathFactory.newInstance().newXPath();
final String expression = "//" + TAG_TRACK_POINT;
final String speedExpression = "//" + TAG_SPEED;

NodeList nodeList = null;
NodeList speedList = null;
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(file);
nodeList = (NodeList) xPath.compile(expression)
.evaluate(document, XPathConstants.NODESET);
speedList = (NodeList) xPath.compile(speedExpression)
.evaluate(document, XPathConstants.NODESET);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
Expand All @@ -286,12 +291,12 @@ public void run() {
e.printStackTrace();
}

parse(nodeList);
parse(nodeList, speedList);
}
}).start();
}

private void parse(NodeList nodeList) {
private void parse(NodeList nodeList, NodeList speedList) {
if (nodeList != null) {
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Expand All @@ -302,6 +307,8 @@ private void parse(NodeList nodeList) {
location.setLatitude(Double.parseDouble(lat));
location.setLongitude(Double.parseDouble(lng));
location.setTime(System.currentTimeMillis());
location.setSpeed(Float.parseFloat(speedList.item(i).getFirstChild()
.getNodeValue()));

new Handler(context.getMainLooper()).post(new Runnable() {
@Override
Expand Down
16 changes: 16 additions & 0 deletions lost/src/test/java/com/mapzen/android/lost/LocationClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,22 @@ public void setMockTrace_shouldInvokeListenerForEachLocation() throws Exception
assertThat(listener.getAllLocations().get(2).getLongitude()).isEqualTo(2.1);
}

@Test
public void setMockTrace_shouldBroadcastSpeedWithLocation() throws Exception {
loadTestGpxTrace();
locationClient.setMockMode(true);
TestLocationListener listener = new TestLocationListener();
LocationRequest request = LocationRequest.create();
request.setFastestInterval(0);
locationClient.requestLocationUpdates(request, listener);
locationClient.setMockTrace("lost.gpx");
Thread.sleep(1000);
Robolectric.runUiThreadTasks();
assertThat(listener.getAllLocations().get(0).getSpeed()).isEqualTo(10f);
assertThat(listener.getAllLocations().get(1).getSpeed()).isEqualTo(20f);
assertThat(listener.getAllLocations().get(2).getSpeed()).isEqualTo(30f);
}

@Test
public void setMockTrace_shouldRespectFastestInterval() throws Exception {
loadTestGpxTrace();
Expand Down
3 changes: 3 additions & 0 deletions lost/src/test/resources/lost.gpx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
<ele>-0.0</ele>
<time>2014-06-10T00:00:00-04:00</time>
</trkpt>
<speed>10.0</speed>
<trkpt lat="1.0" lon="1.1">
<ele>1.0</ele>
<time>2014-06-10T00:00:01-04:00</time>
</trkpt>
<speed>20.0</speed>
<trkpt lat="2.0" lon="2.1">
<ele>2.0</ele>
<time>2014-06-10T00:00:02-04:00</time>
</trkpt>
<speed>30.0</speed>
</trkseg>
</trk>
</gpx>

0 comments on commit 8cc43ea

Please sign in to comment.