如何使用volley从JSON获取经度和纬度?
问题描述:
我想使用排球库来获取特定地址的经度和纬度。尽管我已经能够成功地获得地址,但我没有获得经度和纬度值。如何使用volley从JSON获取经度和纬度?
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);
我该如何解决这个问题?
答
我解决了它这种方式
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为他的贡献
你怎么正好与你的意思是没有得到的纬度和经度?你对地点的意见是什么? –
地点输入是将从用户处获取的地址地址。 – John
请你举个例子,回答我的第一个问题吗?经度和纬度是什么结果? –