public static LocationService getInstance() {
return locationService;
}
public Location getLocationFromAddress(String address) throws Exception {
Location location = new Location();
String encodedAddress = URLEncoder.encode(address, "UTF-8");
URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json?address=" + encodedAddress + "&sensor=false");
String addresses = readURL(url);
JSONObject json = new JSONObject(addresses);
String status = (String) json.get(Mashups4JSFConstants.STATUS_LABEL);
if (Mashups4JSFConstants.OK_MESSAGE.equalsIgnoreCase(status)) {
log.debug("OK Status is: " + status + " for: " + address);
JSONArray results = json.getJSONArray(Mashups4JSFConstants.RESULTS_LABEL);
if (results.length() <= 0) {
return null;
}
JSONObject result = results.getJSONObject(0);
JSONObject jsonLocation = result.getJSONObject(Mashups4JSFConstants.GEOMETRY_LABEL).getJSONObject(Mashups4JSFConstants.LOCATION_LABEL);
Double latitude = (Double) jsonLocation.get(Mashups4JSFConstants.LATITUDE_LABEL);
Double longitude = (Double) jsonLocation.get(Mashups4JSFConstants.LONGITUDE_LABEL);
location.setLatitude(latitude);
location.setLongitude(longitude);
} else {
log.debug("[From Google Service] Status is: " + status);
return null;
}