首页 > 解决方案 > 地理位置Json数据不显示

问题描述

我想我在尝试检索地理位置数据的地方写错了代码:添加了所有相关的 api 密钥并启用了相关的 api...

private class GetCoordinates extends AsyncTask<String, Void, String>
    {
        ProgressDialog dialog = new ProgressDialog(AroundMeMapsActivity.this);

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog.setMessage("Please wait...");
            dialog.setCanceledOnTouchOutside(true);
            dialog.show();
        }

        @Override
        protected String doInBackground(String... strings) {
            String response;
            try {
                Toast.makeText(AroundMeMapsActivity.this, "!!!!", Toast.LENGTH_SHORT).show();

                String address = strings[0];
                HttpDataHandler http = new HttpDataHandler();
                //String url = String.format("https://maps.googleapis.com/maps/api/geocode/json?adress=%s", address);
                String url = String.format("https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=AIzaSyAzSgqQEZS1K1fowxhUnwxl4hMsKMsLlN4", address);
                response = http.getHTTPData(url);
                return response;
            } catch (Exception ex) {

            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            try {
                Toast.makeText(AroundMeMapsActivity.this, "????", Toast.LENGTH_SHORT).show();
                JSONObject jsonObject=new JSONObject(s);

                String lat=((JSONArray)jsonObject.get("results")).getJSONObject(0).getJSONObject("geometry")
                        .getJSONObject("location").get("lat").toString();

                String lng=((JSONArray)jsonObject.get("results")).getJSONObject(0).getJSONObject("geometry")
                        .getJSONObject("location").get("lng").toString();

                answer_location.setText(String.format("Coordinates: %s / %s",lat,lng));
                if(dialog.isShowing())
                    dialog.dismiss();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

我应该得到纬度和经度坐标,但它没有显示......

你能帮忙看看为什么吗?

标签: androidapigoogle-mapsgeocoding

解决方案


推荐阅读