Created
December 16, 2013 09:25
-
-
Save hgoebl/7984391 to your computer and use it in GitHub Desktop.
Example using DavidWebb with Google-API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.goebl.david; | |
import org.json.JSONArray; | |
import org.json.JSONObject; | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
/** | |
* Real world testing a Google API. | |
* | |
* @author goebl | |
* @since 15.12.13 | |
*/ | |
public class TestWithGoogleApi { | |
// this is a replacement for com.google.android.maps.GeoPoint | |
private static class GeoPoint { | |
final int latitudeE6; | |
final int longitudeE6; | |
private GeoPoint(int latitudeE6, int longitudeE6) { | |
this.latitudeE6 = latitudeE6; | |
this.longitudeE6 = longitudeE6; | |
} | |
private GeoPoint(double latitude, double longitude) { | |
this.latitudeE6 = (int) (latitude * 1e6); | |
this.longitudeE6 = (int) (longitude * 1e6); | |
} | |
public int getLatitudeE6() { | |
return latitudeE6; | |
} | |
public int getLongitudeE6() { | |
return longitudeE6; | |
} | |
public String toString() { | |
return (latitudeE6 / 1e6) + "," + (longitudeE6 / 1e6); | |
} | |
} | |
@Test | |
public void getDistance() throws Exception { | |
GeoPoint src = new GeoPoint(47.8227, 12.096933); | |
GeoPoint dest = new GeoPoint(47.8633, 12.215533); | |
Webb webb = Webb.create(); | |
JSONObject result = webb | |
.get("http://maps.googleapis.com/maps/api/directions/json") | |
.param("origin", src) | |
.param("destination", dest) | |
.param("mode", "walking") | |
.param("sensor", "true") | |
.asJsonObject() | |
.getBody(); | |
assertNotNull(result); | |
// System.out.println(result.toString(2)); | |
JSONArray array = result.getJSONArray("routes"); | |
JSONObject routes = array.getJSONObject(0); | |
JSONArray legs = routes.getJSONArray("legs"); | |
JSONObject steps = legs.getJSONObject(0); | |
JSONObject distance = steps.getJSONObject("distance"); | |
int iDistance = distance.getInt("value"); | |
// System.out.println(distance.toString()); | |
assertEquals("distance ~11.6km", iDistance / 1000f, 11.6f, 1f); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment