0

I have successfully returns result of a place details with the code below:

private PlaceDetails getPlaceDetails(String url) throws Exception{


        String jsonResults =null;
        HttpHelper helper = new HttpHelper();
        try {
            jsonResults = helper.GetJSonString(url);
            // Create a JSON object hierarchy from the results

             JSONObject jsonObj = new JSONObject(jsonResults.toString()).getJSONObject("result");

             placeDetails = new PlaceDetails();
             placeDetails.setName(jsonObj.getString("name"));
             placeDetails.setFormatted_address(jsonObj.getString("formatted_address"));
             placeDetails.setInternational_phone_number(jsonObj.getString("international_phone_number"));



        } catch (JSONException e) {
            Log.e("LOG_TAG", "Error processing JSON results " + e.getMessage().toString(), e);
        }

        return placeDetails;
    }

Now, my problem is I dont know how to get lat and lng values out of the result.

Thanks in advance.

  • http://stackoverflow.com/questions/11251023/how-to-get-latitude-and-longitude-in-android – Digit Aug 28 '13 at 10:40

2 Answers2

0

In your response you get lat lon

"geometry" : {
     "location" : {
       "lat" : -33.8669710,
       "lng" : 151.1958750
     }

you only have to parse the data using JsonArray like this

jsonObj.getJsonArray("geometry");

you can refer this link http://yuvislm.wordpress.com/2012/09/10/google-places-api-and-json-parsing-in-android/ for the complete code

Aman
  • 220
  • 3
  • 9
  • I have used this code JSONObject jsonObj1 = new JSONObject(jsonResults.toString()); String lat = ((JSONArray)jsonObj1.get("result")).getJSONObject(0).getJSONObject("geometry") .getJSONObject("location").getString("lat"); but its not still working. – Mark Adesina Omoniyi Aug 28 '13 at 13:41
0

You have parse response as follw

JSONObject jsonObj = new JSONObject(jsonResults.toString());
JSONObject result =jsonObj.getJSONObject("result").getJSONObject("geometry").getJSONObject("location");
Double longitude  = result.getDouble("lng");
Double latitude =  result.getDouble("lat");

As you will get response like

"geometry" : {
     "location" : {
       "lat" : -33.8669710,
       "lng" : 151.1958750
     }

To see response Details

https://developers.google.com/places/web-service/details#PlaceDetailsResults

Anisuzzaman Babla
  • 6,510
  • 7
  • 36
  • 53