Reputation: 1
I am trying to develop an android application that display markers representing all the trees around my area. Being new to Android Development, I am not sure how to retrieve the data from Json. I am only able to display 1 tree, and its name. What I want to accomplish is display all the trees along with their names. Any help/suggestion would greatly appreciated.Below is the Json representation.
{"Tree_ID":"19",
"Tree_Name":"Freeman Maple",
"Latitudes":"41.60659790",
"Longitudes":"-88.07947396"},
{"Tree_ID":"20",
"Tree_Name":"Ginkgo ",
"Latitudes":"41.60653306",
"Longitudes":"-88.07937155"},
{"Tree_ID":"21",
"Tree_Name":"Golden Raintree",
"Latitudes":"41.60644056",
"Longitudes":"-88.07604981",},
{"Tree_ID":"22",
"Tree_Name":"Greenspire Linden",
"Latitudes":"41.60560806",
"Longitudes":"-88.08078320"},
Upvotes: 0
Views: 541
Reputation: 6140
You can get data from JSON
like below.
private class AsyncTaskGetData extends AsyncTask<String, String, JSONArray> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected JSONArray doInBackground(String... arg0) {
JSONArray jsonArray = your webservices goes here....
}
@Override
protected void onPostExecute(JSONArray result) {
if (result != null) {
for (int i = 0; i < result.length(); i++) {
JSONObject dataObject = null;
try {
dataObject = result.getJSONObject(i);
String str1 = dataObject.getString("Tree_ID");
String treename= dataObject.getString("Tree_Name");
String latitude = dataObject.getString("Latitudes");
String longitude = dataObject.getString("Longitudes");
// Call this method for draw marker in google map
drawMarker(new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude)),treename);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
// And call this AsyncTask like below from onCreate() or onResume() or from where you want.
new AsyncTaskGetData().execute();
Add that data in ArrayList
and and use where you want.
For set multiple Markers in Map
// This Method drawmarker in Google Map
private void drawMarker(LatLng point,String text){
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(point).title(text).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
googleMap.addMarker(markerOptions)
}
Upvotes: 1