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

Commit

Permalink
Merge pull request #9 from mapzen/chuck/need-for-speed
Browse files Browse the repository at this point in the history
I have a need...
  • Loading branch information
baldur committed Sep 30, 2014
2 parents 40b85e8 + 027448d commit 4a28cca
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lost-sample/res/layout/list_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/speed"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/time"
android:layout_width="match_parent"
Expand Down
4 changes: 4 additions & 0 deletions lost-sample/src/main/java/com/example/lost/LostActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,24 +181,28 @@ public static void populateLocationView(View view, Location location) {
final TextView provider = (TextView) view.findViewById(R.id.provider);
final TextView coordinates = (TextView) view.findViewById(R.id.coordinates);
final TextView accuracy = (TextView) view.findViewById(R.id.accuracy);
final TextView speed = (TextView) view.findViewById(R.id.speed);
final TextView time = (TextView) view.findViewById(R.id.time);

provider.setText(location.getProvider() + " provider");
coordinates.setText(String.format("%.4f, %.4f", location.getLatitude(),
location.getLongitude()));
accuracy.setText("within " + Math.round(location.getAccuracy()) + " meters");
speed.setText(location.getSpeed() + " m/s");
time.setText(new Date(location.getTime()).toString());
}

public static void clearLocationView(View view) {
final TextView provider = (TextView) view.findViewById(R.id.provider);
final TextView coordinates = (TextView) view.findViewById(R.id.coordinates);
final TextView accuracy = (TextView) view.findViewById(R.id.accuracy);
final TextView speed = (TextView) view.findViewById(R.id.speed);
final TextView time = (TextView) view.findViewById(R.id.time);

provider.setText("");
coordinates.setText("");
accuracy.setText("");
speed.setText("");
time.setText("");
}

Expand Down
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 @@ -7,14 +7,17 @@
<trkpt lat="0.0" lon="0.1">
<ele>-0.0</ele>
<time>2014-06-10T00:00:00-04:00</time>
<speed>10.0</speed>
</trkpt>
<trkpt lat="1.0" lon="1.1">
<ele>1.0</ele>
<time>2014-06-10T00:00:01-04:00</time>
<speed>20.0</speed>
</trkpt>
<trkpt lat="2.0" lon="2.1">
<ele>2.0</ele>
<time>2014-06-10T00:00:02-04:00</time>
<speed>30.0</speed>
</trkpt>
</trkseg>
</trk>
Expand Down

0 comments on commit 4a28cca

Please sign in to comment.