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); } } } 

1 answer 1

I have nothing against JSONObject , but there are more comfortable bikes.

Add the GSON library to the project in the gradle

 dependencies { compile 'com.google.code.gson:gson:2.6.2' } 

Fine.

Now create the GeoPoints class.

 public class GeoPoints { private ArrayList<PointsData> Points; public ArrayList<PointsData> getPoints() { return Points; } static public class PointsData { private double latitude; private double longitude; public double getLatitude() { return latitude; } public double getLongitude() { return longitude; } } } 

Excellent in this facility, we will store all the coordinates.

Now simplify your AsyncTask to disgrace;

  private class GetPoints extends AsyncTask<Void, Void, MyPoints> { 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 MyPoints 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); return new GsonBuilder().create().fromJson(jsonStr, MyPoints.class); } @Override protected void onPostExecute(MyPoints result) { super.onPostExecute(result); // Dismiss the progress dialog if (pDialog.isShowing()) pDialog.dismiss(); /* Updating parsed JSON data into ListView */ //тут создаем кастомный адпатер для ListView } } 

The ParseJSON method can be simply deleted.

Now about how to calculate the distance to the point. First you need to get the current coordinates of the device, this will be the starting point. The calculations themselves may look like this

 float[] arrayOfFloat = new float[1]; Location.distanceBetween(startLatitude, startLongitude, andLatitude, andLongitude, arrayOfFloat); float distance = arrayOfFloat[0]; //дистанция в метрах 

Or like this

 Location startLocation = new Location("startLocation"); startLocation.setLatitude(startLatitude); startLocation.setLongitude(startLongitude); Location endLocation = new Location("endLocation"); endLocation.setLatitude(endlatitude); endLocation.setLongitude(endlongitude); float distance = startLocation.distanceTo(endLocation); 

In any of startLatitude and startLongitude , the location is the position of the device, and the endlatitude and endlongitude coordinates are from your JSON, well, distance is the distance in meters.

And of course, for all this you need to do a Кастомный adapter for the listview ;) to work with the MyPoints class and calculate the distance. But I would strongly recommend that you do ВСЕ calculations in AsyncTask (in the doInBackground method), and doInBackground готовые results to the list.