【问题标题】:Reverse GeoCoordinate Class gives Location Not found error反向地理坐标类给出位置未找到错误
【发布时间】:2015-09-04 21:23:13
【问题描述】:

我在我的应用程序中使用谷歌反向地理编码 API,我成功地能够使用谷歌地理定位 API 获取坐标。现在我正在尝试从反向地理编码 API 获取位置名称,但总是返回 Location not found 错误

这是我的代码:

  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 Geocoder APi 密钥添加到我的应用程序中 - 现在我只有 Geolocation API 密钥用于检索坐标

如果我错了,请纠正我,请提出建议,以便我的代码可以正常工作

谢谢

【问题讨论】:

  • 1.取决于 api,但每个请求都会花费配额。通常,您需要一个密钥才能访问 api,以便跟踪和识别您的使用情况。其他时候,它可能是通过您的 IP。 2. 只是一个特定的请求还是所有的请求? 3. Android Geocoder 无需 api 密钥即可使用。
  • 感谢您的信息,我得到每个坐标的位置未找到错误,我正在使用 Geocoder api,我无法从坐标中获取位置,你能分享任何有反向的好链接吗api 或 google map v3 api android 示例@Andy

标签: android api google-maps google-maps-api-3 reverse-geocoding


【解决方案1】:

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

 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" />

【讨论】:

    猜你喜欢
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    相关资源
    最近更新 更多