0

I use google place api webservice in my android app and I'm new in android and can't to read the JSON result from api , I print this result

{
    "location": {
        "lng": -73.9834889,
        "lat": 40.7724641
    }
}

and

{
    "viewport": {
        "southwest": {
            "lng": -74.03514899999999,
            "lat": 40.698078
        },
        "northeast": {
            "lng": -73.90331300000001,
            "lat": 40.820045
        }
    },
    "location": {
        "lng": -73.9712488,
        "lat": 40.7830603
    }
}

when I write this code

JSONObject dataObj = new JSONObject(response);
String status = dataObj.getString("status");
Log.i("status",status);
if (status.equals("OK")) {
    JSONObject jsonArray =dataObj.getJSONObject("results");
    Log.i("result",""+jsonArray);
    JSONArray aa=dataObj.getJSONArray("results");
    Log.i("resultssss",""+aa);
    ArrayList<Mosque> mosque = new ArrayList<Mosque>();
    for (int i = 0; i < aa.length(); i++) {
        JSONObject jsonObject = aa.getJSONObject(i);
        String geometry=jsonObject.getString("geometry");
        Log.i("geometry",geometry);
Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74
Amal Kronz
  • 1,657
  • 1
  • 18
  • 22

3 Answers3

2

Here is link that might useful to you..

get coordinates from JSON from google maps

And second link link might clear your idea from getting detail from JSON.. http://www.tutorialspoint.com/android/android_json_parser.htm

Hope it might help you.. :D

Community
  • 1
  • 1
1

Well u can parse the google places api response and extract Latitude and Longitude as below.

try {
    JSONObject jObject = new JSONObject(response);
    JSONArray jResults = jObject.getJSONArray("results");

    for (int j = 0; j < jResults.length(); j++) {
        JSONObject jPlaceObj = jResults.getJSONObject(j);
        JSONObject jGeometry =  jPlaceObj.getJSONObject("geometry");
        JSONObject jLocation = jGeometry.getJSONObject("location");

        String lat = jLocation.getString("lat");
        String lng = jLocation.getString("lng");
    }


} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
kevz
  • 2,727
  • 14
  • 39
0
try {
        JSONObject jObject = new JSONObject(response);
        JSONArray jResults = jObject.getJSONArray("results");
        JSONObject jPlaceObj = jResults.getJSONObject(0);
        JSONObject jGeometry =  jPlaceObj.getJSONObject("geometry");
        JSONObject jLocation = jGeometry.getJSONObject("location");

        String lat = jLocation.getString("lat");
        String lng = jLocation.getString("lng");

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Aditay Kaushal
  • 1,021
  • 7
  • 17