private Place buildPlace(JSONObject json) { Place place = new Place(); place.setAddressString(json.getJSONObject("address_obj").getString("address_string")); Coordinates coord = new Coordinates(json.getDouble("latitude"), json.getDouble("longitude")); if (coord.getLat() != 0 || coord.getLon() != 0) { place.setCoord(coord); } place.setId(json.getString("location_id")); place.setName(json.getString("name")); Rating rating = new Rating(); rating.setRating(json.getDouble("rating")); rating.setRevisions(json.getInt("num_reviews")); place.setRating(rating); if (json.has("price_level") && !json.isNull("price_level")) { place.setPriceLevel(json.getString("price_level").length()); } else { place.setPriceLevel(-1); } place.setPhotoId(place.getId()); place.setProvider(providerId); return place; }
private String retrieveLocationID(Coordinates coord) throws IOException { StringBuilder urlBuilder = new StringBuilder(urlMapping); urlBuilder.append(coord.getLat()); urlBuilder.append(","); urlBuilder.append(coord.getLon()); urlBuilder.append("?lang="); urlBuilder.append(language); urlBuilder.append("&key="); urlBuilder.append(apiKey); String urlString = urlBuilder.toString(); String genreJson = restProvider.retrieveRawInformation(urlString); JSONObject json = new JSONObject(genreJson); JSONArray ancestors = json.getJSONArray("data").getJSONObject(0).getJSONArray("ancestors"); String locationID = null; boolean city = false; String cityLocation = ""; boolean island = false; String islandLocation = ""; for (int i = 0; i < ancestors.length() && !(city && island); i++) { JSONObject ancestor = ancestors.getJSONObject(i); if (ancestor.getString("level").equals("City") || ancestor.getString("level").equals("Municipality")) { city = true; cityLocation = ancestor.getString("location_id"); } else if (ancestor.getString("level").equals("Island")) { island = true; islandLocation = ancestor.getString("location_id"); } } if (island) { locationID = islandLocation; } else if (city) { locationID = cityLocation; } return locationID; }