开发者

Geting json response from Google places API in android

My simple android application uses google places api to get the latitude and longitude of a place. I am getting the json response from the google api as below

 {
   "results" : [

      {
         "address_components" : [
            {
               "long_name" : "Chennai",
               "short_name" : "Chennai",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Chennai",
               "short_name" : "Chennai",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Tamil Nadu",
               "short_name" : "TN",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Chennai, Tamil Nadu, India",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 13.23408510,
                  "lng" : 80.33229129999999
               },
               "southwest" : {
                  "lat" : 12.94592670,
     开发者_如何学Python             "lng" : 80.14878270
               }
            },
            "location" : {
               "lat" : 13.0604220,
               "lng" : 80.2495830
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 13.14736150,
                  "lng" : 80.37764240
               },
               "southwest" : {
                  "lat" : 12.97345190,
                  "lng" : 80.12152360
               }
            }
         },
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

Now i want to get the this part in the json response,

"location" : {
           "lat" : 13.0604220,
           "lng" : 80.2495830
        }

I only get the whole "geometry" part as string through this method

JSONObject e = address.getJSONObject(0);
e.getString("geometry")

How to use the only location values. Any help is appreciated. I'm not sure what is going wrong.


JSONObject e = address.getJSONObject(0);
JSONObject jsonLocation = e.getJSONObject("geometry").getJSONObject("location");

and use

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

to get latitude and longitude.


You can also get location values as follows:

For Longitude:

Double lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lng");

For Latitude:

Double lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lat");


Try this. It worked for me.

JSONObject result = jsonObject.getJSONObject("result");

Double lat = result.getJSONObject("geometry").getJSONObject("location").getDouble("lat");
Double lng = result.getJSONObject("geometry").getJSONObject("location").getDouble("lng");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜