反向地理坐标 Class 给出 Location Not found 错误

Reverse GeoCoordinate Class gives Location Not found error

我在我的应用程序中使用 google 反向地理编码 API,我成功地能够使用 google 地理定位 API 获取坐标。现在我正在尝试从反向地理编码 API 获取位置名称,但总是 returns 找不到位置错误

这是我的代码:

  Geocoder geocoder= new Geocoder(MainActivity.this, Locale.ENGLISH);
    

  if(geocoder.isPresent()){
          List<Address> list;
 try {
   list = geocoder.getFromLocation(37.42279, -122.08506,1);
    Address address = list.get(0);
    Log.d("this is working","thsi sis working");

          StringBuffer str = new StringBuffer();
          str.append("Name: " + address.getLocality() + "\n");
          str.append("Sub-Admin Ares: " + address.getSubAdminArea() + "\n");
          str.append("Admin Area: " + address.getAdminArea() + "\n");
          str.append("Country: " + address.getCountryName() + "\n");
          str.append("Country Code: " + address.getCountryCode() + "\n");;

          String strAddress = str.toString();
          JsonDatas.setText(strAddress);
           Log.d("Address", strAddress);
  } 
    catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

我有几个问题

  1. 如何 google 知道哪个应用请求了 API 以及 google 如何确定该应用的配额。在 google API 开发者控制台中,google 为应用 API
  2. 提供配额
  3. 为什么我收到“找不到位置”错误,但搜索 google 地图位置却显示
  4. 我是否需要将 google Geocoder APi 键添加到我的应用程序中 - 现在我只有 Geolocation API 键用于检索坐标

如果我错了,请指正,请提出建议,这样我的代码才能正常工作

感谢

确保您的项目中有最新的 Google 播放库。我正在使用此代码并为我工作。

 private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String> {
        Context mContext;

        public ReverseGeocodingTask(Context context) {
            super();
            mContext = context;
        }

        // Finding address using reverse geocoding
        @Override
        protected String doInBackground(LatLng... params) {
            Geocoder geocoder = new Geocoder(mContext);
            double latitude = params[0].latitude;
            double longitude = params[0].longitude;
            List<Address> addresses = null;
            String addressText = "";

            try {

                addresses = geocoder.getFromLocation(latitude, longitude, 1);

                Thread.sleep(500);

                if (addresses != null && addresses.size() > 0) {

                    Address address = addresses.get(0);

                    addressText = String.format(
                            "%s",
                            address.getMaxAddressLineIndex() > 0 ? address
                                    .getAddressLine(1) : "", "", "");
                }
            } catch (IOException e) {
                e.printStackTrace();

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

            return addressText;
        }

        @Override
        protected void onPostExecute(String addressText) {
        }
}

LatLng 是纬度和经度,检查 this 文档。

并使用写下new ReverseGeocodingTask(this).execute(latlng);你想要获取数据的地方。

确保您在清单文件中添加权限。

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />