【问题标题】:Get Longitude and Latitude values from Google Geocoding API in Android Studio with Java使用 Java 从 Android Studio 中的 Google Geocoding API 获取经度和纬度值
【发布时间】:2015-03-26 07:42:43
【问题描述】:

这是我的 getCurrentLocation 方法:

private CurrentLocation getCurrentLocation(String jsonData) throws JSONException {
    JSONObject object = new JSONObject(jsonData);
    JSONArray results = object.getJSONArray("results");

    JSONObject objects = results.getJSONObject(0);
    JSONObject geometry = objects.getJSONObject("geometry");
    JSONObject location = geometry.getJSONObject("location");
    //instance of CurrentLocation class
    CurrentLocation currentLocation = new CurrentLocation();
    currentLocation.setLongitude(location.getDouble("lat"));
    currentLocation.setLatitude(location.getDouble("lng")); 
    Log.i(TAG, "FROM JSON: " + currentLocation.getLatitude());
    Log.i(TAG, "FROM JSON: " + currentLocation.getLongitude());
return currentLocation;
}

从 JSON 中打印出来:-117.9145036 FROM JSON: 33.8352932 这是正确的,

我得到一个 NullPointerException 的错误就行了:

 private void updateLocation() {
       getForecast(mCurrentLocation.getLatitude(),mCurrentLocation.getLongitude());
}

我知道 private void getForecast(double latitude, double longitude) 有效,因为我已经输入了两个双精度参数并且它有效。 我对 android 还是很陌生,我知道工作室本身有一个地理定位器,但我想通过 API 来做。我不确定 nullException 指向什么,任何帮助将不胜感激。谢谢。

如果有帮助,这里是我的其他方法: 创建时:

This is my Oncreate method:

public void onClick(View view) {
             String name = mNameField.getText().toString();
             getLocation(name);
             updateLocation(); // error here?

和获取位置:

public void getLocation(String name) {
    //get Google api
    String geoLocatingUrl = "https://maps.googleapis.com/maps/api/geocode/json?address="+name+"&key=AIzaSyAsJJIEeB952KpjpoFoMuYHd6gev85uzYs";

        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(geoLocatingUrl)
                .build();

        Call call = client.newCall(request);
        //UI thread executes enqueue, sends to OKHttp Library gets data, response or failure back to UI
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

        @Override
        public void onResponse(Response response) throws IOException {

            try {
                   String jsonData = response.body().string();
                if (response.isSuccessful()) {

                    mCurrentLocation = getCurrentLocation(jsonData);
                    Log.v(TAG, jsonData);

                } else {
                    alertUserAboutError();

                }
            } catch (IOException e) {
                Log.e(TAG, "Exception caught: ", e);

            }
            catch (JSONException e) {
            }
        }
    });
}

和getForecast:

private void getForecast(double latitude, double longitude) {
    String apiKey = "//myapikey";

    String forecastUrl =  "https://api.forecast.io/forecast/" + apiKey +
            "/" + latitude + "," + longitude;

    if (isNetworkAvailable()) { //runs if network is there and available
        toggleRefresh();
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(forecastUrl)
                .build();

        Call call = client.newCall(request);
        //UI thread executes enqueue, sends to OKHttp Library gets data, response or failure back to UI
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        toggleRefresh();
                    }
                });
                alertUserAboutError();
            }

            @Override
            public void onResponse(Response response) throws IOException {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        toggleRefresh();
                    }
                });
                try {
                    String jsonData = response.body().string();
                    if (response.isSuccessful()) {

                        mCurrentWeather = getCurrentDetails(jsonData);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                updateDisplay();
                            }
                        });

                    } else {
                        alertUserAboutError();
                    }
                }
                catch (IOException e) {
                }
                catch (JSONException e) {
                }
            }
        });
    }
    else {
        Toast.makeText(this, getString(R.string.
                        network_unavailable_message),
                Toast.LENGTH_LONG
        ).show();
    }
}

【问题讨论】:

  • 您正在打印整个对象,请尝试打印特定数据,例如 currentLocation.getLatitude()。

标签: java android api google-maps


【解决方案1】:

我假设您正在尝试打印“CurrentLocation”类的对象。所以值是@42c49c78。你要做的就像

 Log.i(TAG, "FROM JSON: " + currentLocation.getLatitude());

希望对你有帮助。

【讨论】:

  • 是的,那部分打印出纬度。我试图弄清楚为什么它在 updateLocation 方法上打印出 nullpointerException 谢谢你的帮助(:
  • 您必须发布您的 getForecast() 方法。没有它很难回答。
  • 我发布了,但如果它有效,这是否意味着问题出在其他地方?如果我做 getForecast(33.15351, -127.251521) 它会显示任何城市的天气
  • 请发布完整的 logcat。意味着有必要找出导致空指针异常的行。
  • getForecast(mCurrentLocation.getLatitude(),mCurrentLocation.getLongitude());那条线
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-04
  • 1970-01-01
相关资源
最近更新 更多