How to get latitude and longitude from JSON using volley?

Home / Uncategorized / How to get latitude and longitude from JSON using volley?

Question:
I am trying to use volley library to get longitude and latitude of a particular address. Though I have been able to get the address successfully, but I am not getting the latitude and longitude values.
String googleMapUrl = "http://maps.googleapis.com/maps/api/geocode/json?address="+ place + "&sensor=false";

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, googleMapUrl, null, new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
try {
Toast.makeText(MainActivity.this, "Response "+ response, Toast.LENGTH_SHORT).show();
// JSONArray res = o.getJSONArray("results");
for (int i = 0; i < response.length(); i++) { //JSONObject jsonObj = new JSONObject(result.toString()); //JSONArray resultJsonArray = jsonObj.getJSONArray("results"); JSONObject jsonObj = (JSONObject) response.get("i"); JSONObject resultJsonArray = jsonObj.getJSONObject("results"); // Extract the Place descriptions from the results // resultList = new ArrayList(resultJsonArray.length());

JSONObject before_geometry_jsonObj = resultJsonArray.getJSONObject("0");

JSONObject geometry_jsonObj = before_geometry_jsonObj
.getJSONObject("geometry");

JSONObject location_jsonObj = geometry_jsonObj
.getJSONObject("location");

String lat_helper = location_jsonObj.getString("lat");
lat = Double.valueOf(lat_helper);

String lng_helper = location_jsonObj.getString("lng");
lng = Double.valueOf(lng_helper);

How do I go about this?


Answer:

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *