Skip to content

Instantly share code, notes, and snippets.

@rkaneko
Last active July 19, 2016 10:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rkaneko/5336260 to your computer and use it in GitHub Desktop.
Save rkaneko/5336260 to your computer and use it in GitHub Desktop.
The Snippet to call Google Geocode API on Play framework(v2.1.0) .
import org.codehaus.jackson.JsonNode;
import play.libs.WS;
import play.libs.F.Promise;
import play.libs.WS.WSRequestHolder;
/**
* Refer
* http://goo.gl/HXhJG
* Javadoc api/2.1.0/java/play/libs/WS.WSRequestHolder : http://goo.gl/AXmBY
* Javadoc api/2.1.0/java/play/libs/F.Promise : http://goo.gl/Ad3ec
*/
public class GeoCode {
/** Endpoint for Google Geocode API . */
private static final String GEOCODE_ENDPOINT = "http://maps.googleapis.com/maps/api/geocode/json";
/**
* Call Google Geocode API .
* @params address : ex)東京都渋谷区宇田川町2-15
* @return results value formatted with JSON
*/
public static JsonNode callGeocode(String address) throws RuntimeException {
WSRequestHolder wsRequestHolder = WS.url(GEOCODE_ENDPOINT);
wsRequestHolder.setQueryParameter("address", address);
wsRequestHolder.setQueryParameter("sensor", "false");
Promise<WS.Response> geocode = wsRequestHolder.get();
final String STATUS = geocode.get().asJson()
.findPath("status").asText();
JsonNode results = null;
if (STATUS.equals("OK") {
results = geocode.get().asJson()
.findPath("results");
return results;
} else {
// TODO more implement .
// Other status value is "ZERO_RESULTS" or "REQUEST_DENIED" .....
throw new RuntimeException("fail to get results .");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment