use of im.actor.map.MapItem in project actor-platform by actorapp.
the class PlaceFetchingTask method doInBackground.
@Override
protected Object doInBackground(Void... voids) {
ArrayList<MapItem> resultList = null;
HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
StringBuilder sb = new StringBuilder(PLACES_API_BASE + METHOD_NAME + OUT_JSON);
sb.append("?key=" + API_KEY);
// sb.append("&radius="+radius);
sb.append("&rankby=distance");
sb.append("&location=" + latitude + "," + longitude);
if (query != null)
sb.append("&keyword=" + URLEncoder.encode(query, "utf8"));
else
sb.append("&types=cafe");
URL url = new URL(sb.toString());
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Load the results into a StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
Log.i(LOG_TAG, "Response: " + jsonResults.toString());
} catch (MalformedURLException e) {
if (conn != null) {
conn.disconnect();
}
Log.e(LOG_TAG, "Error processing Places API URL", e);
return e;
} catch (IOException e) {
if (conn != null) {
conn.disconnect();
}
Log.e(LOG_TAG, "Error connecting to Places API", e);
return e;
} finally {
if (conn != null) {
conn.disconnect();
}
}
try {
// Create a JSON object hierarchy from the results
JSONObject jsonResult = new JSONObject(jsonResults.toString());
JSONArray jsonResultItems = jsonResult.getJSONArray("results");
// Extract the Place descriptions from the results
resultList = new ArrayList<MapItem>(jsonResultItems.length());
for (int i = 0; i < jsonResultItems.length(); i++) {
// todo json parser
final JSONObject jsonResultItem = jsonResultItems.getJSONObject(i);
MapItem item = new MapItem() {
{
id = jsonResultItem.optString("id", null);
name = jsonResultItem.optString("name", null);
vicinity = jsonResultItem.optString("vicinity", null);
icon = jsonResultItem.optString("icon", null);
if (jsonResultItem.has("geometry")) {
geometry = new Geometry();
geometry.location = new Location() {
{
lat = jsonResultItem.optJSONObject("geometry").optJSONObject("location").optDouble("lat", 0.0);
lng = jsonResultItem.optJSONObject("geometry").optJSONObject("location").optDouble("lng", 0.0);
}
};
}
}
};
resultList.add(item);
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Cannot process JSON results", e);
return e;
}
return resultList;
}
Aggregations