如何使用排球从JSON获得经度和纬度?

时间:2017-08-15 06:13:04

标签: json google-maps android-volley

我正在尝试使用排球库来获取特定地址的经度和纬度。虽然我已经能够成功获得地址,但我没有得到纬度和经度值。

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<JSONObject>() {
                @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<String>(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);

我该怎么做?

1 个答案:

答案 0 :(得分:1)

我这样解决了

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, googleMapUrl, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    Toast.makeText(MainActivity.this, "Response "+ response, Toast.LENGTH_SHORT).show();

                    Log.d("Volley Response ", String.valueOf(response));
                    lat = response.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getDouble("lat");
                    lng = response.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getDouble("lng");

感谢Michael的贡献