Hello, please tell me how to extract data (latitude and longitude) with JSON and with the help of Location.distanceTo
you need to calculate and display the distance in leaves ... (it does not work out to implement ( Location.distanceTo
)
public class MainActivity extends ListActivity { // JSON Node names private static final String TAG_POINTS = "Points"; private static final String TAG_NAME = "name"; private static final String TAG_ADDRESS = "address"; private static final String TAG_PARTNER_NAME = "partner_name"; private static final String TAG_LATITUDE = "latitude"; private static final String TAG_LONGITUDE = "longitude"; private static String url = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); new GetPoints().execute(); } private ArrayList<HashMap<String, String>> ParseJSON(String json) { if (json != null) { try { // Hashmap for ListView ArrayList<HashMap<String, String>> PointsList = new ArrayList<HashMap<String, String>>(); JSONObject jsonObj = new JSONObject(json); // Getting JSON Array node JSONArray Points = jsonObj.getJSONArray(TAG_POINTS); ////////////////// // looping through All for (int i = 0; i < Points.length(); i++) { JSONObject c = Points.getJSONObject(i); String name = c.getString(TAG_NAME); String email = c.getString(TAG_ADRESS); String partner_name = c.getString(TAG_PARTNER_NAME); HashMap<String, String> points = new HashMap<String, String>(); points.put(TAG_NAME, name); points.put(TAG_ADRESS, email); points.put(TAG_PARTNER_NAME, partner_name); // adding PointsList.add(points); } return PointsList; } catch (JSONException e) { e.printStackTrace(); return null; } } else { Log.e("ServiceHandler", "Couldn't get any data from the url"); return null; } } /** * Async task class to get json by making HTTP call */ private class GetPoints extends AsyncTask<Void, Void, Void> { // Hashmap for ListView ArrayList<HashMap<String, String>> PointsList; ProgressDialog pDialog; @Override protected void onPreExecute() { super.onPreExecute(); // Showing progress dialog pDialog = new ProgressDialog(MainActivity.this); pDialog.setMessage("Please wait..."); pDialog.setCancelable(false); pDialog.show(); } @Override protected Void doInBackground(Void... arg0) { // Creating service handler class instance WebRequest webreq = new WebRequest(); // Making a request to url and getting response String jsonStr = webreq.makeWebServiceCall(url, WebRequest.GET); Log.d("Response: ", "> " + jsonStr); PointsList = ParseJSON(jsonStr); return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); // Dismiss the progress dialog if (pDialog.isShowing()) pDialog.dismiss(); /* Updating parsed JSON data into ListView */ ListAdapter adapter = new SimpleAdapter( MainActivity.this, PointsList, R.layout.list_item, new String[]{TAG_NAME, TAG_ADRESS, TAG_PARTNER_NAME}, new int[]{R.id.name, R.id.email, R.id.partner_name, R.id.distance}); setListAdapter(adapter); } } }