【问题标题】:Geocoder not working地理编码器不工作
【发布时间】:2012-06-10 17:02:31
【问题描述】:

我有一个 Android 应用程序,我需要从 lat/long 获取地址。我使用了以下代码库:

Geocoder geoCoder = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> address = null;
if (geoCoder != null){
   try {
       address= geoCoder.getFromLocation(51.50, -0.12, 1);
       } catch (IOException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
      }
      if (address.size()> 0){
        String postCode = address.get(0).getPostalCode();
        System.out.println(postCode);
      }
}

我还在清单文件中添加了以下内容

 uses-permission android:name="android.permission.INTERNET"  
 uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"

但是每次我执行这个块时address.size() 仍然是“0”,我也尝试过不同的坐标;什么都没有改变。我正在从 Eclipse 中完成所有这些操作。

【问题讨论】:

    标签: android eclipse avd locationmanager geocode


    【解决方案1】:

    试试这个:

    private class ReverseGeocodingTask extends AsyncTask<Location, Void, Void> {
        Context mContext;
    
        public ReverseGeocodingTask(Context context) {
            super();
            mContext = context;
        }
    
        @Override
        protected Void doInBackground(Location... params) {
            Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
    
            Location loc = params[0];
            List<Address> addresses = null;
            try {
                addresses = geocoder.getFromLocation(loc.getLatitude(),
                        loc.getLongitude(), 1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (addresses != null && addresses.size() > 0) {
                Address address = addresses.get(0);
                // Format the first line of address (if available), city, and
                // country name.
                final String addressText = String.format(
                        "%s, %s, %s",
                        address.getMaxAddressLineIndex() > 0 ? address
                                .getAddressLine(0) : "", address.getLocality(),
                        address.getCountryName());
    
                // Update address field on UI.
                mHandler.post(new Runnable() {
                    public void run() {
                        Toast.makeText(getBaseContext(),
                                addressText.toString(), Toast.LENGTH_SHORT)
                                .show();
                    }
                });
    
            }
            return null;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      相关资源
      最近更新 更多