android - 使用地理编码器和 android Google Maps API v2 获取纬度和经度

标签 android google-maps maps android-maps-v2

我正在使用适用于 android 的 Google Maps API v2 并且可以正常工作。 但是,我正在尝试使用地理编码器来获取地址的经度和纬度,但没有成功。

它改变了 v2 的方式?

我使用的是常规代码

Geocoder gc = new Geocoder(context);
//...
  List<Address> list = gc.getFromLocationName("1600 Amphitheatre Parkway, Mountain View, CA", 1);

  Address address = list.get(0);

  double lat = address.getLatitude();
  double lng = address.getLongitude();
//...

总是返回一个强制关闭,而 Log 什么也解决不了。 使用 try/catch block 时,打开 map 但总是在同一个位置 使用互联网权限,我在项目中也包含了COARSE_LOCATION 我使用了这里和其他网站上的各种代码,但没有成功。

提前谢谢你。

最佳答案

使用此示例 url 尝试此解决方案:

http://maps.google.com/maps/api/geocode/json?address=mumbai&sensor=false

返回json格式的数据,lat/lngaddress

private class DataLongOperationAsynchTask extends AsyncTask<String, Void, String[]> {
   ProgressDialog dialog = new ProgressDialog(MainActivity.this);
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog.setMessage("Please wait...");
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    @Override
    protected String[] doInBackground(String... params) {
        String response;
        try {
            response = getLatLongByURL("http://maps.google.com/maps/api/geocode/json?address=mumbai&sensor=false");
            Log.d("response",""+response);
            return new String[]{response};
        } catch (Exception e) {
            return new String[]{"error"};
        }
    }

    @Override
    protected void onPostExecute(String... result) {
        try {
            JSONObject jsonObject = new JSONObject(result[0]);

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

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

            Log.d("latitude", "" + lat);
            Log.d("longitude", "" + lng);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}


public String getLatLongByURL(String requestURL) {
    URL url;
    String response = "";
    try {
        url = new URL(requestURL);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        conn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        conn.setDoOutput(true);
        int responseCode = conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            String line;
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = br.readLine()) != null) {
                response += line;
            }
        } else {
            response = "";
        }

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

希望对你有帮助。

关于android - 使用地理编码器和 android Google Maps API v2 获取纬度和经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15711499/

相关文章:

java - 我如何将直接从 Google Place API 获取的地址组件明智地 fork ?

javascript - 删除 Google API V3 中的标记

java - 如何使单个按钮在 Android 中显示为 Activity 状态

android - 我无法在 VS 代码中运行和调试 flutter 应用程序,每当我尝试运行它时,我都会收到以下错误

android - 在 Cordova 中使用 Webpack 时出现“找不到文件”错误

javascript - 在 NativeScript 应用程序中向 map 添加标记

javascript - 是否可以捕获 JavaScript 异步回调中抛出的异常?

android - 在Android Studio中导入Gradle项目失败

google-maps - 使用 5k+ 地址的可用地理 API 计算行程旅行时间

javascript - 我可以在 Android 浏览器中仅使用 javascript 启动 Intent 吗?